<?xml version="1.0" encoding="UTF-8"?><rss xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:media="http://search.yahoo.com/mrss/" version="2.0">
    <channel>
        <title>Peter @ WWW</title>
        <link>https://cloudy.nyc</link>
        <description>Personal blog and technical writings</description>
        <generator>Zola</generator>
        <language>en</language>
        <atom:link href="https://cloudy.nyc/rss.xml" rel="self" type="application/rss+xml"/>
        <lastBuildDate>Sun, 20 Sep 2026 00:00:00 +0000</lastBuildDate>
        <item>
            <title>Guerrilla Gardening</title>
            <pubDate>Sun, 20 Sep 2026 00:00:00 +0000</pubDate>
            <author>Peter Valdez</author>
            <link>https://cloudy.nyc/blog/guerilla-gardening/</link>
            <guid>https://cloudy.nyc/blog/guerilla-gardening/</guid>
            <description>This summer, my hobby has been cleaning and beautifying my corner in Astoria.</description>
            <content:encoded xml:base="https://cloudy.nyc/blog/guerilla-gardening/"><![CDATA[<p>This summer, my hobby has been cleaning and beautifying my corner in Astoria.</p>
<p>It began with a mourning dove nest under my AC.</p>
<div class="pair pair-right size-medium justify-evenly">
<div class="pair-media">
<div class="img-row size-medium">
<img src="https://img.cloudy.nyc/guerilla-gardening/cat-and-chicks-a623affb.jpg" alt="Cat and chicks">
</div>
</div>
<div class="pair-text">
<h2 id="dove-nest-bird-mites">Dove nest, bird mites</h2>
<p>The doves had a couple rounds of babies a few weeks apart. After they abandoned it, a bunch of bird mites invaded my AC. It was creepy, but they don't feed on humans or cats, so we were safe.</p>
<p>I got rid of the abandoned nest, which made a mess of twigs in my apartment courtyard. A week passed, and the twigs were still there on the side of the footpath.</p>
<p>I created the mess, so I needed to clean it up. I bought a broom and dustpan, and swept it up.</p>
<p>It was satisfying. I was hooked.</p>
</div>
</div>
<h2 id="courtyard-sidewalk-tree-beds">Courtyard, sidewalk, tree beds</h2>
<p>Litter would build up by the two entrances to my courtyard. Plastic of all sorts, vapes, bags. So I swept them up.</p>
<p>My cleaning then expanded to the sidewalk in front of the apartment building. Lord knows how long some of that litter had been sitting there.</p>
<p>I also started doing little litter patrols in the neighborhood.</p>
<div class="img-row">
<img src="https://img.cloudy.nyc/guerilla-gardening/litter-collection-1.jpg" alt="An overflowing public trash can on the sidewalk, full of litter">
<img src="https://img.cloudy.nyc/guerilla-gardening/litter-collection-bag.jpg" alt="A bag stuffed full of collected litter next to a public trash can">
<img src="https://img.cloudy.nyc/guerilla-gardening/litter-collection-2.jpg" alt="A trash can next to another full public bin during a litter patrol">
</div>
<div class="pair pair-left size-small justify-evenly">
<div class="pair-media">
<div class="video-row size-small">
<video autoplay loop muted playsinline data-sync-loop="4">
<source src="https://img.cloudy.nyc/guerilla-gardening/sweeping-night-loop-32c89139.mp4" type="video/mp4">
</video>
</div>
</div>
<div class="pair-text">
<p>I started cleaning the tree beds in front of the building. I've learned that untended tree beds become de facto trash cans. Lots of small plastic containers, bottle caps, glass.</p>
<p>So I swept it all up.</p>
<p>There are only the city-planted trees and dirty dirt in those tree beds, but they deserve to be cared for.</p>
</div>
</div>
<div class="post-spacer desktop-only"></div>
<div class="pair pair-right size-medium">
<div class="pair-media">
<div class="video-row size-medium">
<video autoplay loop muted playsinline data-sync-loop="4">
<source src="https://img.cloudy.nyc/guerilla-gardening/prepping-bed-loop-68edd3a2.mp4" type="video/mp4">
</video>
<video autoplay loop muted playsinline data-sync-loop="4">
<source src="https://img.cloudy.nyc/guerilla-gardening/digging-dirt-loop-c31c0f9f.mp4" type="video/mp4">
</video>
</div>
</div>
<div class="pair-text">
<h2 id="make-it-pretty-with-bricks">Make it pretty, with bricks</h2>
<p>After a few weeks of this cleaning, it occurred to me that the tree beds were a form of public space. Public gardens that often weren't tended to.</p>
<p>So I thought, why not make them prettier? Have them spark a little joy.</p>
<p>I took a walk around the neighborhood for some inspiration, looked at a few different nice tree bed setups, and decided: a nice simple red brick border. If it didn't go higher than 2 inches, then I wouldn't even need a permit to do so. Perfect.</p>
</div>
</div>
<p>Went to Home Depot, bought 45 bricks, and one Saturday afternoon of hard work later, we had a pretty red brick border on the two tree beds framing my apartment courtyard entrance.</p>
<div class="clear-beside"></div>
<div class="post-spacer desktop-only"></div>
<div class="pair pair-left size-small justify-spread">
<div class="pair-media">
<div class="video-row size-small">
<video autoplay loop muted playsinline data-sync-loop="4">
<source src="https://img.cloudy.nyc/guerilla-gardening/compost-bags-loop-f1b2bdb2.mp4" type="video/mp4">
</video>
</div>
</div>
<div class="pair-text">
<h2 id="make-it-pretty-with-dirt">Make it pretty, with dirt</h2>
<p>The tree beds weren't in their final form yet, though. They still needed green plants. Pretty flowers.</p>
<p>But first, the dirt needed some love.</p>
<p>So after a bunch of unfruitful trips to Home Depot and Big Reuse, I secured some organic compost. From Home Depot, it turns out. The trick was to order pickup so they'd fetch it from ✨the back✨.</p>
<p>4 bags of organic compost per tree bed. I used my new cultivator, and mixed the compost into the tree beds. It's amazing how even just fresh, rich dirt alone made the tree beds look better. Healthier.</p>
</div>
</div>
<div class="post-spacer desktop-only"></div>
<div class="pair pair-right size-medium">
<div class="pair-media">
<div class="img-row size-medium">
<img src="https://img.cloudy.nyc/guerilla-gardening/flowers-in-trunk.jpg" alt="The trunk of a rented SUV packed with potted purple and orange flowers and lilyturf">
</div>
</div>
<div class="pair-text">
<h2 id="make-it-pretty-with-flowers">Make it pretty, with flowers</h2>
<p>Litter, cleaned regularly. Bricks, laid. Organic compost, all set.</p>
<p>This left one thing: pretty flowers.</p>
<p>I rented a Toyota 4Runner through Zipcar and made another visit to Home Depot.</p>
<p>Bought twelve lilyturf, four orange aster, four light purple aster. To be split between the two tree beds.</p>
</div>
</div>
<p>After another Saturday afternoon of manual labor, with the help of my lovely girlfriend Anne and my friend David, the plants were planted.</p>
<div class="clear-beside"></div>
<div class="video-row size-full">
<video autoplay loop muted playsinline data-sync-loop="4">
<source src="https://img.cloudy.nyc/guerilla-gardening/flowers-1-loop-24c0fc0e.mp4" type="video/mp4">
</video>
<video autoplay loop muted playsinline data-sync-loop="4">
<source src="https://img.cloudy.nyc/guerilla-gardening/sweeping-1-loop-a0d4cda0.mp4" type="video/mp4">
</video>
<video autoplay loop muted playsinline data-sync-loop="4">
<source src="https://img.cloudy.nyc/guerilla-gardening/flowers-2-loop-7b763169.mp4" type="video/mp4">
</video>
</div>
<h2 id="why">Why</h2>
<p>We deserve beautiful spaces, joyful streets. These tree beds are public gardens waiting to be tended to.</p>
<p>When we live in a city like New York, we leave the neighborhood's upkeep to the city. "That's what our taxes are for, right? Street cleaning and parks?"</p>
<p>But that's a lot to ask. There's too much street and soil for the city to watch over, and it can't take care of a block the way the people who live there can.</p>
<p>If we're willing to accept that responsibility, we can have nice things: beautiful gardens, clean streets, and third places for people to gather.</p>
<p>I'm really happy with how the tree beds turned out, and I'm glad to report that neighbors have told me they're happy with them too.</p>
<p>One guy told me: "They're beautiful, I've never seen anything like that in front of the building in the 30 years I've been here." I really thought that was beautiful.</p>
<p>A few days later, another neighbor told me they came out very pretty, and that she hasn't seen anything like that in front of the building in the 50 (!!) years that she's been here. Now that's wild.</p>
<p>We're really out here changing the city.</p>
<p><img src="https://img.cloudy.nyc/guerilla-gardening/tree-bed-with-flowers-and-painted-stones-f5c8b319.jpg" alt="Tree bed with flowers and painted stones" /></p>
]]></content:encoded>
            <media:content url="https://img.cloudy.nyc/guerilla-gardening/tree-bed-with-flowers-and-painted-stones-f5c8b319.jpg" medium="image"/>
            <media:thumbnail url="https://img.cloudy.nyc/guerilla-gardening/tree-bed-with-flowers-and-painted-stones-f5c8b319.jpg"/>
        </item>
        <item>
            <title>localmart: why, what, and a visualization</title>
            <pubDate>Sat, 28 Jun 2025 00:00:00 +0000</pubDate>
            <author>Peter Valdez</author>
            <link>https://cloudy.nyc/blog/localmart-visualization/</link>
            <guid>https://cloudy.nyc/blog/localmart-visualization/</guid>
            <description>For the first couple months of the year, I was working on an idea called localmart.</description>
            <content:encoded xml:base="https://cloudy.nyc/blog/localmart-visualization/"><![CDATA[<h2 id="why-what">why &amp; what</h2>
<p>For the first couple months of the year, I was working on an idea called <strong>localmart</strong>.</p>
<p>The motivation behind of <strong>localmart</strong> is this:</p>
<ol>
<li>Lots of people today order things from Amazon that they could get in their neighborhood.</li>
<li>That means that money is being extracted from local neighborhoods into global corporations.</li>
<li>So what if we were able to provide the <a href="http://Amazon.com">Amazon.com</a> experience, but all the goods are sourced from local stores so that your money gets reinvested into your neighborhood?</li>
</ol>
<p>So in short, <strong>localmart</strong> is a service where you order local goods from local stores, and it gets delivered within a day or two.</p>
<h2 id="visualization">visualization</h2>
<p>I've had a visualization in mind to explain <strong>localmart</strong> to folks, so here's a quick version.</p>
<p>Cash is flowing out to corporations, but with more local orders, cash stays in the neighborhood and makes the neighborhood healthier.</p>
<div class="bleed">
<div id="localmart-visualization" style="width: 100%; height: min(64vh, 760px); min-height: 380px; margin: 2rem 0; position: relative; background: linear-gradient(135deg, #87CEEB 0%, #98FB98 100%);">
    <div id="controls" style="position: absolute; top: 20px; right: max(20px, calc(50% - 440px)); z-index: 100; background: rgba(255,255,255,0.9); padding: 12px; border-radius: 6px; box-shadow: 0 2px 10px rgba(0,0,0,0.1); width: 180px;">
        <div style="margin-bottom: 10px; font-size: 13px; font-weight: 700; color: #333; text-align: center;">Orders in Astoria</div>
        <div style="margin-bottom: 10px; font-size: 12px; color: #444; text-align: center; font-weight: 600;">
            <span id="current-time">June 2025</span>
        </div>
        <div id="money-stats" style="margin-bottom: 10px;">
            <div style="margin-bottom: 6px;">
                <div style="font-size: 11px; font-weight: 600; color: #555; margin-bottom: 1px;">To corporations:</div>
                <div id="money-out" style="font-size: 13px; font-weight: bold; color: #d32f2f; text-align: right;">$0</div>
            </div>
            <div>
                <div style="font-size: 11px; font-weight: 600; color: #555; margin-bottom: 1px;">To neighborhood:</div>
                <div id="money-in" style="font-size: 13px; font-weight: bold; color: #2e7d32; text-align: right;">$0</div>
            </div>
        </div>
        <div>
            <label for="redirect-slider" style="display: block; margin-bottom: 4px; font-size: 11px; color: #555; font-weight: 600;">Local Orders:</label>
            <input type="range" id="redirect-slider" min="0" max="100" value="0" style="width: 100%; margin-bottom: 4px;">
            <div style="font-size: 12px; color: #666; text-align: center; font-weight: 500;"><span id="slider-value">0%</span></div>
        </div>
    </div>
    <canvas id="neighborhood-canvas" style="width: 100%; height: 100%; display: block;"></canvas>
</div>
</div>
<script src="https://cdnjs.cloudflare.com/ajax/libs/three.js/r128/three.min.js"></script>
<script>
class NeighborhoodVisualization {
    constructor() {
        this.container = document.getElementById('localmart-visualization');
        this.canvas = document.getElementById('neighborhood-canvas');
        this.redirectSlider = document.getElementById('redirect-slider');
        this.sliderValue = document.getElementById('slider-value');
        this.moneyOutSpan = document.getElementById('money-out');
        this.moneyInSpan = document.getElementById('money-in');
        this.currentTimeSpan = document.getElementById('current-time');
        
        this.scene = new THREE.Scene();
        this.camera = new THREE.PerspectiveCamera(75, this.container.offsetWidth / this.container.offsetHeight, 0.1, 1000);
        this.renderer = new THREE.WebGLRenderer({ canvas: this.canvas, antialias: true, alpha: true });
        
        this.houses = [];
        this.moneyStreams = [];
        this.localStores = [];
        this.redirectionRate = 0; // 0 to 1, how much money stays local
        this.isRedirecting = false;
        
        // Money tracking for cumulative display
        this.corporateMoney = 0;
        this.neighborhoodMoney = 0;
        this.lastUpdateTime = Date.now();
        
        // Time tracking - starting June 2025
        this.currentMonth = 6; // June
        this.currentYear = 2025;
        this.monthNames = ['Jan', 'Feb', 'Mar', 'Apr', 'May', 'Jun', 'Jul', 'Aug', 'Sep', 'Oct', 'Nov', 'Dec'];
        this.lastMonthUpdate = Date.now();
        
        // High-impact visualization economics for Astoria
        // Astoria's estimated Amazon order volume
        this.monthlyAmazonSpending = 5000000; // $5M per month - Astoria's estimated Amazon order volume
        this.monthDurationMs = 2000; // 2 seconds per month
        
        // Flourishing tracking
        this.flourishingLevel = 0; // 0 to 4 levels of flourishing
        this.addedTrees = [];
        this.addedFlowers = [];
        this.addedHouses = [];
        this.addedParks = [];
        this.addedBusinesses = [];
        this.addedCommunitySpaces = [];
        this.ground = null; // Store reference for grass color updates
        
        this.init();
    }
    
    init() {
        // Set up renderer
        this.renderer.setSize(this.container.offsetWidth, this.container.offsetHeight);
        this.renderer.setClearColor(0x87CEEB, 0);
        this.renderer.shadowMap.enabled = true;
        this.renderer.shadowMap.type = THREE.PCFSoftShadowMap;
        
        // Set up camera (fixed isometric angle, distance fits the box)
        this.fitCamera();
        
        // Add lighting
        const ambientLight = new THREE.AmbientLight(0x404040, 0.6);
        this.scene.add(ambientLight);
        
        const directionalLight = new THREE.DirectionalLight(0xffffff, 0.8);
        directionalLight.position.set(10, 20, 5);
        directionalLight.castShadow = true;
        directionalLight.shadow.mapSize.width = 2048;
        directionalLight.shadow.mapSize.height = 2048;
        this.scene.add(directionalLight);
        
        // Create neighborhood
        this.createGround();
        this.createRoads();
        this.createTrees();
        this.createHouses();
        this.createLocalStores();
        this.createCorporateDistrict();
        
        // Set up controls
        this.redirectSlider.addEventListener('input', (e) => this.onSliderChange(e));
        
        // Start money streams immediately
        this.startMoneyStreams();
        this.createLocalStreams();
        this.localStreamsCreated = true;
        
        // Set initial visibility (all corporate, no local)
        this.updateStreamVisibility();
        
        // Initialize displays
        this.updateTimeDisplay();
        this.updateMoneyDisplay();
        
        // Start animation
        this.animate();
        
        // Handle resize
        window.addEventListener('resize', () => this.onWindowResize());
    }
    
    createGround() {
        const groundGeometry = new THREE.PlaneGeometry(46, 46); // Reduced to match neighborhood size
        const groundMaterial = new THREE.MeshLambertMaterial({ color: 0x8DB360 }); // Start with muted olive grass
        this.ground = new THREE.Mesh(groundGeometry, groundMaterial);
        this.ground.rotation.x = -Math.PI / 2;
        this.ground.receiveShadow = true;
        this.scene.add(this.ground);
    }
    
    createRoads() {
        const roadMaterial = new THREE.MeshLambertMaterial({ color: 0x444444 });
        
        // Create a grid of roads for dense urban layout
        // Horizontal roads (east-west)
        for (let z = -20; z <= 20; z += 8) {
            const road = new THREE.Mesh(new THREE.PlaneGeometry(46, 2.5), roadMaterial);
            road.rotation.x = -Math.PI / 2;
            road.position.set(0, 0.01, z);
            this.scene.add(road);
        }
        
        // Vertical roads (north-south)
        for (let x = -20; x <= 20; x += 8) {
            const road = new THREE.Mesh(new THREE.PlaneGeometry(2.5, 46), roadMaterial);
            road.rotation.x = -Math.PI / 2;
            road.position.set(x, 0.01, 0);
            this.scene.add(road);
        }
        
        // Yellow center lines
        this.createRoadLines();
    }
    
    createRoadLines() {
        const lineMaterial = new THREE.MeshLambertMaterial({ color: 0xFFFF00 });
        
        // Center lines for horizontal roads
        for (let z = -20; z <= 20; z += 8) {
            for (let x = -22; x <= 22; x += 3) {
                const line = new THREE.Mesh(new THREE.PlaneGeometry(2, 0.1), lineMaterial);
                line.rotation.x = -Math.PI / 2;
                line.position.set(x, 0.02, z);
                this.scene.add(line);
            }
        }
        
        // Center lines for vertical roads
        for (let x = -20; x <= 20; x += 8) {
            for (let z = -22; z <= 22; z += 3) {
                const line = new THREE.Mesh(new THREE.PlaneGeometry(0.1, 2), lineMaterial);
                line.rotation.x = -Math.PI / 2;
                line.position.set(x, 0.02, z);
                this.scene.add(line);
            }
        }
    }
    
    createTrees() {
        // Scatter trees throughout the neighborhood more naturally - fill edges
        const treePositions = [
            // Corner trees
            [-22, -22], [-22, 22], [22, -22], [22, 22],
            // Edge trees to fill border completely
            [-22, -15], [-22, -8], [-22, 0], [-22, 8], [-22, 15],
            [22, -15], [22, -8], [22, 0], [22, 8], [22, 15],
            [-15, -22], [-8, -22], [0, -22], [8, -22], [15, -22],
            [-15, 22], [-8, 22], [0, 22], [8, 22], [15, 22],
            // Inner border trees
            [-15, -18], [15, -18], [-15, 18], [15, 18],
            [-18, -10], [-18, 10], [18, -10], [18, 10],
            [-10, -22], [10, -22], [-10, 22], [10, 22],
            [-5, -15], [5, -15], [-5, 15], [5, 15],
            [-22, -5], [-22, 5], [22, -5], [22, 5]
        ];
        
        treePositions.forEach(pos => {
            if (Math.random() > 0.2) { // 80% chance to place a tree for better coverage
                this.createTree(pos[0], pos[1]);
            }
        });
    }
    
    createTree(x, z) {
        const group = new THREE.Group();
        
        // Tree trunk
        const trunkGeometry = new THREE.CylinderGeometry(0.1, 0.15, 1);
        const trunkMaterial = new THREE.MeshLambertMaterial({ color: 0x8B4513 });
        const trunk = new THREE.Mesh(trunkGeometry, trunkMaterial);
        trunk.position.y = 0.5;
        trunk.castShadow = true;
        group.add(trunk);
        
        // Tree foliage
        const foliageGeometry = new THREE.SphereGeometry(0.8, 8, 8);
        const foliageMaterial = new THREE.MeshLambertMaterial({ color: 0x228B22 });
        const foliage = new THREE.Mesh(foliageGeometry, foliageMaterial);
        foliage.position.y = 1.5;
        foliage.castShadow = true;
        group.add(foliage);
        
        group.position.set(x, 0, z);
        this.scene.add(group);
    }
    
    createHouses() {
        // Create dense urban blocks - fill each city block with buildings
        // Skip center area to keep it clear for community spaces
        const blockCenters = [];
        for (let x = -16; x <= 16; x += 8) {
            for (let z = -16; z <= 16; z += 8) {
                // Skip the center square (keep -8 to +8 area clear for flourishing)
                if (Math.abs(x) > 6 || Math.abs(z) > 6) {
                    blockCenters.push([x, z]);
                }
            }
        }
        
        let houseIndex = 0;
        blockCenters.forEach(blockCenter => {
            // Create 4-6 buildings per block in different positions
            const buildingsInBlock = [
                [blockCenter[0] - 2, blockCenter[1] - 2],
                [blockCenter[0] + 2, blockCenter[1] - 2],
                [blockCenter[0] - 2, blockCenter[1] + 2],
                [blockCenter[0] + 2, blockCenter[1] + 2],
                [blockCenter[0], blockCenter[1] - 2.5],
                [blockCenter[0], blockCenter[1] + 2.5]
            ];
            
            buildingsInBlock.forEach(pos => {
                // Double-check to avoid center area completely
                if ((Math.abs(pos[0]) > 7 || Math.abs(pos[1]) > 7) && Math.random() > 0.6) { // 40% chance to place a building (start sparse)
                    const house = this.createHouse(pos[0], pos[1], houseIndex);
                    this.houses.push(house);
                    houseIndex++;
                }
            });
        });
    }
    
    createHouse(x, z, index) {
        const group = new THREE.Group();
        
        // Random building type
        const buildingType = Math.random();
        // Animal Crossing bright primary colors - cozy but vibrant!
        const colors = [
            0xFF6B9D, // Bright pink
            0x4FC3F7, // Bright sky blue
            0xFFB74D, // Orange  
            0x66BB6A, // Bright green
            0xFFEB3B, // Bright yellow
            0xBA68C8, // Bright purple
            0xFF8A65, // Coral
            0x26C6DA, // Bright cyan
            0x8BC34A, // Lime green
            0xF06292, // Hot pink
            0x42A5F5, // Electric blue
            0xFFA726  // Bright orange
        ];
        
        if (buildingType < 0.3) {
            // Apartment building (taller)
            const height = 3 + Math.random() * 2;
            const houseGeometry = new THREE.BoxGeometry(1.8, height, 1.8);
            const houseMaterial = new THREE.MeshLambertMaterial({ color: colors[index % colors.length] });
            const house = new THREE.Mesh(houseGeometry, houseMaterial);
            house.position.y = height / 2;
            house.castShadow = true;
            group.add(house);
            
            // Flat roof with bright colors
            const roofGeometry = new THREE.BoxGeometry(1.9, 0.2, 1.9);
            const roofColors = [0xD84315, 0xFF6F00, 0x8D6E63, 0x5D4037, 0xBF360C]; // Bright warm roof colors
            const roofColor = roofColors[index % roofColors.length];
            const roofMaterial = new THREE.MeshLambertMaterial({ color: roofColor });
            const roof = new THREE.Mesh(roofGeometry, roofMaterial);
            roof.position.y = height + 0.1;
            roof.castShadow = true;
            group.add(roof);
            
            // Add cute chimney for cozy feel
            if (Math.random() > 0.4) { // 60% chance for chimney
                const chimneyGeometry = new THREE.BoxGeometry(0.3, 0.8, 0.3);
                const chimneyMaterial = new THREE.MeshLambertMaterial({ color: 0xD84315 }); // Bright brick red
                const chimney = new THREE.Mesh(chimneyGeometry, chimneyMaterial);
                chimney.position.set(0.6, height + 0.5, 0.6);
                chimney.castShadow = true;
                group.add(chimney);
            }
            
        } else if (buildingType < 0.6) {
            // Row house / townhouse
            const houseGeometry = new THREE.BoxGeometry(1.2, 2, 1.5);
            const houseMaterial = new THREE.MeshLambertMaterial({ color: colors[index % colors.length] });
            const house = new THREE.Mesh(houseGeometry, houseMaterial);
            house.position.y = 1;
            house.castShadow = true;
            group.add(house);
            
            // Slanted roof with bright colors
            const roofGeometry = new THREE.ConeGeometry(1, 0.6, 4);
            const roofColors = [0xD84315, 0xFF6F00, 0x8D6E63, 0x5D4037, 0xBF360C]; // Bright warm roof colors
            const roofColor = roofColors[index % roofColors.length];
            const roofMaterial = new THREE.MeshLambertMaterial({ color: roofColor });
            const roof = new THREE.Mesh(roofGeometry, roofMaterial);
            roof.position.y = 2.3;
            roof.rotation.y = Math.PI / 4;
            roof.castShadow = true;
            group.add(roof);
            
            // Add cute chimney for townhouse
            if (Math.random() > 0.3) { // 70% chance for chimney
                const chimneyGeometry = new THREE.BoxGeometry(0.25, 0.7, 0.25);
                const chimneyMaterial = new THREE.MeshLambertMaterial({ color: 0xD84315 }); // Bright brick red
                const chimney = new THREE.Mesh(chimneyGeometry, chimneyMaterial);
                chimney.position.set(0.4, 2.8, 0.4);
                chimney.castShadow = true;
                group.add(chimney);
            }
            
        } else {
            // Regular house
            const houseGeometry = new THREE.BoxGeometry(1.5, 1.5, 1.5);
            const houseMaterial = new THREE.MeshLambertMaterial({ color: colors[index % colors.length] });
            const house = new THREE.Mesh(houseGeometry, houseMaterial);
            house.position.y = 0.75;
            house.castShadow = true;
            group.add(house);
            
            // Peaked roof with bright colors
            const roofGeometry = new THREE.ConeGeometry(1.2, 0.8, 4);
            const roofColors = [0xD84315, 0xFF6F00, 0x8D6E63, 0x5D4037, 0xBF360C]; // Bright warm roof colors
            const roofColor = roofColors[index % roofColors.length];
            const roofMaterial = new THREE.MeshLambertMaterial({ color: roofColor });
            const roof = new THREE.Mesh(roofGeometry, roofMaterial);
            roof.position.y = 1.9;
            roof.rotation.y = Math.PI / 4;
            roof.castShadow = true;
            group.add(roof);
            
            // Add cute chimney for regular house
            if (Math.random() > 0.5) { // 50% chance for chimney
                const chimneyGeometry = new THREE.BoxGeometry(0.2, 0.6, 0.2);
                const chimneyMaterial = new THREE.MeshLambertMaterial({ color: 0xD84315 }); // Bright brick red
                const chimney = new THREE.Mesh(chimneyGeometry, chimneyMaterial);
                chimney.position.set(0.5, 2.5, 0.5);
                chimney.castShadow = true;
                group.add(chimney);
            }
        }
        
        group.position.set(x, 0, z);
        this.scene.add(group);
        
        return { group, position: { x, z }, moneyGeneration: Math.random() * 0.5 + 0.3 };
    }
    
    createLocalStores() {
        const storeData = [
            { pos: [-14, -10], type: 'coffee', name: 'Cozy Coffee' },
            { pos: [6, -14], type: 'pet', name: 'Pet Paradise' },
            { pos: [-6, 10], type: 'bakery', name: 'Sweet Treats' },
            { pos: [14, 6], type: 'bookstore', name: 'Corner Books' },
            { pos: [-18, 2], type: 'pharmacy', name: 'Local Pharmacy' },
            { pos: [2, -18], type: 'deli', name: 'Neighborhood Deli' },
            { pos: [18, -2], type: 'grocery', name: 'Corner Market' },
            { pos: [-2, 18], type: 'laundry', name: 'Quick Wash' }
        ];
        
        storeData.forEach((data, index) => {
            const store = this.createLocalStore(data.pos[0], data.pos[1], data.type, data.name);
            this.localStores.push(store);
        });
    }
    
    createLocalStore(x, z, type, name) {
        const group = new THREE.Group();
        
        // Store base
        const storeGeometry = new THREE.BoxGeometry(2.5, 2, 2.5);
        let storeColor;
        switch(type) {
            case 'coffee': storeColor = 0x8D6E63; break; // Rich coffee brown
            case 'pet': storeColor = 0xFF6B9D; break; // Bright pink
            case 'bakery': storeColor = 0xFFB74D; break; // Bright orange
            case 'bookstore': storeColor = 0x4FC3F7; break; // Bright sky blue
            case 'pharmacy': storeColor = 0x66BB6A; break; // Bright green
            case 'deli': storeColor = 0xF06292; break; // Hot pink
            case 'grocery': storeColor = 0xBA68C8; break; // Bright purple
            case 'laundry': storeColor = 0x26C6DA; break; // Bright cyan
            default: storeColor = 0x8BC34A; // Lime green
        }
        
        const storeMaterial = new THREE.MeshLambertMaterial({ color: storeColor });
        const store = new THREE.Mesh(storeGeometry, storeMaterial);
        store.position.y = 1;
        store.castShadow = true;
        group.add(store);
        
        // Bright roof
        const roofGeometry = new THREE.ConeGeometry(1.8, 0.6, 4);
        const roofMaterial = new THREE.MeshLambertMaterial({ color: 0x8D6E63 }); // Brighter brown
        const roof = new THREE.Mesh(roofGeometry, roofMaterial);
        roof.position.y = 2.3;
        roof.rotation.y = Math.PI / 4;
        roof.castShadow = true;
        group.add(roof);
        
        // Store sign
        const signGeometry = new THREE.BoxGeometry(2.8, 0.4, 0.1);
        const signMaterial = new THREE.MeshLambertMaterial({ color: 0xF8F8FF });
        const sign = new THREE.Mesh(signGeometry, signMaterial);
        sign.position.y = 2.5;
        sign.position.z = 1.3;
        group.add(sign);
        
        // Add type-specific decorations
        if (type === 'coffee') {
            // Coffee cup decoration
            const cupGeometry = new THREE.CylinderGeometry(0.1, 0.1, 0.2);
            const cupMaterial = new THREE.MeshLambertMaterial({ color: 0xFFFFFF });
            const cup = new THREE.Mesh(cupGeometry, cupMaterial);
            cup.position.set(0.5, 2.7, 1.3);
            group.add(cup);
        } else if (type === 'pet') {
            // Pet bone decoration
            const boneGeometry = new THREE.BoxGeometry(0.3, 0.1, 0.1);
            const boneMaterial = new THREE.MeshLambertMaterial({ color: 0xFFFACD });
            const bone = new THREE.Mesh(boneGeometry, boneMaterial);
            bone.position.set(-0.5, 2.7, 1.3);
            group.add(bone);
        }
        
        group.position.set(x, 0, z);
        this.scene.add(group);
        
        return { group, position: { x, z }, type, name };
    }
    
    createCorporateDistrict() {
        this.corporateBuildings = [];
        
        // Create multiple corporate buildings along the back left edge with more spacing
        const buildingPositions = [
            { x: -40, z: -20, name: 'Amazon' },
            { x: -40, z: -8, name: 'Walmart' },
            { x: -40, z: 4, name: 'Target' },
            { x: -40, z: -32, name: 'Best Buy' },
            { x: -40, z: 16, name: 'Home Depot' }
        ];
        
        buildingPositions.forEach((pos, index) => {
            const building = this.createCorporateBuilding(pos.x, pos.z, pos.name, index);
            this.corporateBuildings.push(building);
        });
        
        // Add "CORPORATIONS" text above the buildings
        this.addCorporationsText();
    }
    
    addCorporationsText() {
        // Create a canvas for the text
        const canvas = document.createElement('canvas');
        const context = canvas.getContext('2d');
        
        // Set canvas size (larger for better quality at 3x size)
        canvas.width = 1024;
        canvas.height = 256;
        
        // Set text properties (no background fill for transparency)
        context.fillStyle = '#2c3e50';
        context.font = 'bold 120px Arial, sans-serif';
        context.textAlign = 'center';
        context.textBaseline = 'middle';
        
        // Draw the text (no background rectangle)
        context.fillText('CORPORATIONS', canvas.width / 2, canvas.height / 2);
        
        // Create texture from canvas
        const texture = new THREE.CanvasTexture(canvas);
        
        // Create material and geometry for the text
        const textMaterial = new THREE.MeshLambertMaterial({ 
            map: texture,
            transparent: true,
            alphaTest: 0.1
        });
        
        const textGeometry = new THREE.PlaneGeometry(19.2, 4.8); // 80% of previous size
        const textMesh = new THREE.Mesh(textGeometry, textMaterial);
        
        // Position the text above the corporate buildings (adjusted height for bigger sign)
        textMesh.position.set(-40, 17, -8);
        textMesh.lookAt(0, 20, 0); // Make it face towards the neighborhood
        
        this.scene.add(textMesh);
    }
    
    addWindowsToBuilding(group, width, height, depth) {
        const windowMaterial = new THREE.MeshLambertMaterial({ color: 0x87CEEB }); // Light blue windows
        const windowSize = 0.3;
        const windowSpacing = 0.8;
        
        // Calculate how many windows can fit on each side
        const windowsPerRow = Math.floor(width / windowSpacing) - 1;
        const windowRows = Math.floor(height / windowSpacing) - 1;
        
        // Front face windows (facing towards city)
        for (let row = 0; row < windowRows; row++) {
            for (let col = 0; col < windowsPerRow; col++) {
                const windowGeometry = new THREE.BoxGeometry(windowSize, windowSize, 0.05);
                const window = new THREE.Mesh(windowGeometry, windowMaterial);
                
                const x = (col - (windowsPerRow - 1) / 2) * windowSpacing;
                const y = (row - (windowRows - 1) / 2) * windowSpacing + height / 2;
                const z = depth / 2 + 0.025;
                
                window.position.set(x, y, z);
                group.add(window);
            }
        }
        
        // Left side windows
        for (let row = 0; row < windowRows; row++) {
            for (let col = 0; col < windowsPerRow; col++) {
                const windowGeometry = new THREE.BoxGeometry(0.05, windowSize, windowSize);
                const window = new THREE.Mesh(windowGeometry, windowMaterial);
                
                const x = -width / 2 - 0.025;
                const y = (row - (windowRows - 1) / 2) * windowSpacing + height / 2;
                const z = (col - (windowsPerRow - 1) / 2) * windowSpacing;
                
                window.position.set(x, y, z);
                group.add(window);
            }
        }
        
        // Right side windows
        for (let row = 0; row < windowRows; row++) {
            for (let col = 0; col < windowsPerRow; col++) {
                const windowGeometry = new THREE.BoxGeometry(0.05, windowSize, windowSize);
                const window = new THREE.Mesh(windowGeometry, windowMaterial);
                
                const x = width / 2 + 0.025;
                const y = (row - (windowRows - 1) / 2) * windowSpacing + height / 2;
                const z = (col - (windowsPerRow - 1) / 2) * windowSpacing;
                
                window.position.set(x, y, z);
                group.add(window);
            }
        }
    }
    
    createCorporateBuilding(x, z, name, index) {
        const group = new THREE.Group();
        
        // Varying heights and sizes for different corporations (bigger buildings)
        const heights = [12, 10, 11, 9, 14];
        const widths = [6, 5.5, 7, 5, 8];
        // Softer corporate colors - still professional but less harsh
        const colors = [0x4A5568, 0x5A6C7D, 0x6B7280, 0x718096, 0x2D3748];
        
        const height = heights[index % heights.length];
        const width = widths[index % widths.length];
        const color = colors[index % colors.length];
        
        // Corporate building (tall, imposing, bigger)
        const buildingGeometry = new THREE.BoxGeometry(width, height, width);
        const buildingMaterial = new THREE.MeshLambertMaterial({ color });
        const building = new THREE.Mesh(buildingGeometry, buildingMaterial);
        building.position.y = height / 2;
        building.castShadow = true;
        group.add(building);
        
        // Add windows to the building
        this.addWindowsToBuilding(group, width, height, width);
        
        // Corporate logo area (facing towards the city)
        const logoGeometry = new THREE.BoxGeometry(width + 0.1, 1.5, 0.1);
        const logoColors = [0xf39c12, 0x3498db, 0xe74c3c, 0xffff00, 0xff6600];
        const logoMaterial = new THREE.MeshLambertMaterial({ color: logoColors[index % logoColors.length] });
        const logo = new THREE.Mesh(logoGeometry, logoMaterial);
        logo.position.y = height - 1.5;
        logo.position.z = -(width / 2 + 0.05); // Negative Z to face towards the city
        group.add(logo);
        
        group.position.set(x, 0, z);
        this.scene.add(group);
        
        return { group, position: { x, z }, name };
    }
    
    createMoneyStream(fromPos, toPos, color = 0xf1c40f, isLocal = false) {
        const stream = {
            particles: [],
            fromPos,
            toPos,
            color,
            active: true,
            isLocal
        };
        
        // Create particles for the stream (bigger dollar bill-like particles)
        for (let i = 0; i < 4; i++) {
            // Create a flatter rectangular shape for dollar bills (bigger and more visible)
            const geometry = new THREE.BoxGeometry(0.6, 0.3, 0.05);
            const material = new THREE.MeshLambertMaterial({ 
                color,
                transparent: false,
                opacity: 1.0
            });
            const particle = new THREE.Mesh(geometry, material);
            particle.castShadow = true;
            
            const progress = i / 4;
            particle.position.lerpVectors(
                new THREE.Vector3(fromPos.x, 2, fromPos.z),
                new THREE.Vector3(toPos.x, 6, toPos.z),
                progress
            );
            
            // Add some rotation for animation
            particle.userData = { 
                progress, 
                speed: 0.008 + Math.random() * 0.005,
                rotationSpeed: (Math.random() - 0.5) * 0.15,
                initialRotation: Math.random() * Math.PI * 2
            };
            
            stream.particles.push(particle);
            this.scene.add(particle);
        }
        
        return stream;
    }
    
    updateMoneyStreams() {
        // Calculate time once per frame instead of per particle
        const time = Date.now() * 0.001;
        
        // Reuse Vector3 objects to avoid creating new ones every frame
        if (!this.tempFromPos) {
            this.tempFromPos = new THREE.Vector3();
            this.tempToPos = new THREE.Vector3();
        }
        
        this.moneyStreams.forEach(stream => {
            if (!stream.active) return;
            
            // Set up positions once per stream
            this.tempFromPos.set(stream.fromPos.x, 2, stream.fromPos.z);
            this.tempToPos.set(stream.toPos.x, 6, stream.toPos.z);
            
            stream.particles.forEach(particle => {
                particle.userData.progress += particle.userData.speed;
                
                if (particle.userData.progress > 1) {
                    particle.userData.progress = 0;
                }
                
                particle.position.lerpVectors(this.tempFromPos, this.tempToPos, particle.userData.progress);
                
                // Add some curve to the trajectory (higher arc)
                particle.position.y += Math.sin(particle.userData.progress * Math.PI) * 3;
                
                // Animate rotation for dollar bill effect (using cached time)
                particle.rotation.z = particle.userData.initialRotation + time * particle.userData.rotationSpeed;
                particle.rotation.y = Math.sin(time * 2 + particle.userData.initialRotation) * 0.3;
            });
        });
    }
    
    startMoneyStreams() {
        this.houses.forEach(house => {
            // Randomly select a corporate building to send money to
            const randomCorp = this.corporateBuildings[Math.floor(Math.random() * this.corporateBuildings.length)];
            const corpStream = this.createMoneyStream(
                house.position,
                randomCorp.position,
                0x2ecc71 // Green for all money
            );
            this.moneyStreams.push(corpStream);
        });
    }
    
    onSliderChange(event) {
        const value = parseInt(event.target.value);
        this.sliderValue.textContent = value + '%';
        
        // Set redirection rate based on slider (0-100% -> 0-1.0)
        this.redirectionRate = value / 100;
        this.updateRedirection();
        
        // Update stream visibility to show correct proportion
        this.updateStreamVisibility();
    }
    
    updateMoneyAccumulation() {
        const currentTime = Date.now();
        const deltaTime = (currentTime - this.lastUpdateTime) / 1000; // Convert to seconds
        this.lastUpdateTime = currentTime;
        
        // High-impact money flow: $6M per month over 3 seconds
        // $6M / 3 seconds = $2M per second for dramatic visual impact
        const moneyPerSecond = this.monthlyAmazonSpending / (this.monthDurationMs / 1000);
        
        // Distribute money based on redirection rate
        const corporateFlow = moneyPerSecond * (1 - this.redirectionRate) * deltaTime;
        const neighborhoodFlow = moneyPerSecond * this.redirectionRate * deltaTime;
        
        this.corporateMoney += corporateFlow;
        this.neighborhoodMoney += neighborhoodFlow;
        
        // Update time progression (advance 1 month every 3 seconds of real time)
        if (currentTime - this.lastMonthUpdate > this.monthDurationMs) {
            this.advanceMonth();
            this.lastMonthUpdate = currentTime;
        }
        
        // Update display periodically (every ~100ms for smooth updates)
        if (currentTime % 100 < 50) {
            this.updateMoneyDisplay();
            this.updateTimeDisplay();
            
            // Check for flourishing changes as money accumulates
            this.updateNeighborhoodFlourishing();
        }
    }
    
    addFlourishingTrees(count) {
        const possiblePositions = [
            // TREES - Avoiding all community spaces
            // Community space exclusions:
            // - Plaza area [0,0] ± 3 units  
            // - Garden area [-4,4] ± 3 units
            // - West Park [-15,-15] ± 4 units  
            // - East Park [15,15] ± 4 units
            // - Legacy basketball [0,-12] ± 3 units
            
            // Safe edge positions
            [-18, -10], [18, -10], [-10, -18], [10, -18],
            [-18, 10], [18, 10], [-10, 18], [10, 18],
            [-18, -6], [18, -6], [-6, -18], [6, -18],
            [-18, 6], [18, 6], [-6, 18], [6, 18],
            [-18, -2], [18, -2], [-2, -18], [2, -18],
            [-18, 2], [18, 2], [-2, 18], [2, 18],
            // Additional safe edge trees
            [-16, -18], [16, -18], [-18, -16], [18, -16],
            [-16, 18], [16, 18], [-18, 16], [18, 16],
            [-14, -18], [14, -18], [-18, -14], [18, -14],
            [-14, 18], [14, 18], [-18, 14], [18, 14],
            [-12, -18], [12, -18], [-18, -12], [18, -12],
            [-12, 18], [12, 18], [-18, 12], [18, 12],
            // Safe inner positions (avoiding community spaces)
            [-9, -16], [9, -16], [-16, -9], [16, -9],
            [-9, 16], [9, 16], [-16, 9], [16, 9],
            [-5, -18], [5, -18], [-18, -5], [18, -5],
            [-5, 18], [5, 18], [-18, 5], [18, 5],
            // Additional safe coverage
            [-8, -18], [8, -18], [-18, -8], [18, -8],
            [-8, 18], [8, 18], [-18, 8], [18, 8],
            // Corners away from parks
            [-19, -9], [19, -9], [-9, -19], [9, -19],
            [-19, 9], [19, 9], [-9, 19], [9, 19],
            // Far corners  
            [-19, -5], [19, -5], [-5, -19], [5, -19],
            [-19, 5], [19, 5], [-5, 19], [5, 19],
            // Safe mid-edge positions
            [-17, -7], [17, -7], [-7, -17], [7, -17],
            [-17, 7], [17, 7], [-7, 17], [7, 17]
        ];
        
        for (let i = 0; i < count && this.addedTrees.length < possiblePositions.length; i++) {
            const pos = possiblePositions[this.addedTrees.length];
            this.createTree(pos[0], pos[1]);
            this.addedTrees.push(pos);
        }
    }
    
    addFlowers(count) {
        const flowerPositions = [
            // FLOWERS - Avoiding all community spaces
            // Community space exclusions:
            // - Plaza area [0,0] ± 3 units (x: -3 to 3, z: -3 to 3)
            // - Garden area [-4,4] ± 3 units (x: -7 to -1, z: 1 to 7)  
            // - West Park [-15,-15] ± 4 units (x: -19 to -11, z: -19 to -11)
            // - East Park [15,15] ± 4 units (x: 11 to 19, z: 11 to 19)
            // - Legacy basketball [0,-12] ± 3 units (x: -3 to 3, z: -15 to -9)
            
            // Safe edge positions - avoiding all exclusion zones
            [-18, -8], [18, -8], [-8, -18], [8, -18],
            [-18, 8], [18, 8], [-8, 18], [8, 18],
            [-16, -6], [16, -6], [-6, -16], [6, -16],
            [-16, 6], [16, 6], [-6, 18], [6, 18],
            [-5, -17], [5, -17], [-17, -5], [17, -5],
            [-5, 17], [5, 17], [-17, 5], [17, 5],
            // Safe positions away from community spaces
            [-10, -18], [10, -18], [-18, -10], [18, -10],
            [-18, 10], [18, 10], [-10, 18], [10, 18],
            [-16, -2], [16, -2], [-16, 2], [16, 2],
            [-18, -2], [18, -2], [-18, 2], [18, 2],
            [-18, -6], [18, -6], [-6, -18], [6, -18],
            [-18, 6], [18, 6], [-6, 16], [6, 16],
            [-19, -9], [19, -9], [-9, -19], [9, -19],
            [-19, 9], [19, 9], [-9, 19], [9, 19],
            [-18, -12], [18, -12], [-12, -18], [12, -18],
            [-18, 12], [18, 12], [-12, 18], [12, 18],
            [-19, -7], [19, -7], [-7, -19], [7, -19],
            [-19, 7], [19, 7], [-7, 17], [7, 17],
            [-19, -5], [19, -5], [-5, -19], [5, -19],
            [-19, 5], [19, 5], [-5, 17], [5, 17],
            [-19, -3], [19, -3], [-19, 3], [19, 3],
            // Safe mid-range positions
            [-17, -7], [17, -7], [-7, -17], [7, -17],
            [-17, 7], [17, 7],
            // Safe scattered positions throughout non-excluded areas
            [-10, -6], [10, -6], [-10, 6], [10, 6],
            [-14, -8], [14, -8], [-8, -17], [8, -17],
            [-14, 8], [14, 8], [-8, 17], [8, 17],
            [-12, -6], [12, -6], [-12, 6], [12, 6],
            [-16, -10], [16, -10], [-10, -16], [10, -16],
            [-16, 10], [16, 10], [-10, 16], [10, 16],
            // Additional safe flower positions
            [-9, -17], [9, -17], [-17, -9], [17, -9],
            [-9, 17], [9, 17], [-17, 9], [17, 9],
            [-8, -16], [8, -16], [-16, -8], [16, -8],
            [-8, 16], [8, 16], [-16, 8], [16, 8]
        ];
        
        for (let i = 0; i < count && this.addedFlowers.length < flowerPositions.length; i++) {
            const pos = flowerPositions[this.addedFlowers.length];
            this.createFlower(pos[0], pos[1]);
            this.addedFlowers.push(pos);
        }
    }
    
    addNewBusinesses(count) {
        const businessPositions = [
            // Strategic business locations - avoiding all community spaces
            // Community space exclusions:
            // - Plaza [0,0] ± 3, Garden [-4,4] ± 3, West Park [-15,-15] ± 4, East Park [15,15] ± 4, Basketball [0,-12] ± 3
            [-10, -14], [10, -14], [-14, -10], [14, -10],
            [-6, -18], [6, -18], [-18, -6], [18, -6],
            [-16, -2], [16, -2], // Removed conflicting positions near plaza/garden
            [-14, -6], [14, -6], [-6, -14], [6, -14], 
            [-18, -10], [18, -10], [-10, -18], [10, -18],
            [-12, -16], [12, -16], [-16, -12], [16, -12],
            [-8, -2], [8, -2], // Moved away from plaza area
            [-18, -8], [18, -8], [-8, -18], [8, -18], // Safe edge positions
            [-14, -2], [14, -2], [-2, -14], [2, -14], // Safe from all community spaces
            [-10, -16], [10, -16], [-16, -10], [16, -10] // Additional safe business spots
        ];
        
        const businessTypes = [
            { type: 'restaurant', name: 'Local Bistro', color: 0xFF6B9D }, // Bright pink
            { type: 'cafe', name: 'Corner Café', color: 0x8D6E63 }, // Rich coffee brown
            { type: 'fitness', name: 'Neighborhood Gym', color: 0x42A5F5 }, // Electric blue
            { type: 'boutique', name: 'Local Boutique', color: 0xBA68C8 }, // Bright purple
            { type: 'hardware', name: 'Hardware Store', color: 0xFFEB3B }, // Bright yellow
            { type: 'florist', name: 'Flower Shop', color: 0xFF8A65 }, // Coral
            { type: 'artgallery', name: 'Art Gallery', color: 0x26C6DA }, // Bright cyan
            { type: 'music', name: 'Music Store', color: 0xF06292 }, // Hot pink
            { type: 'wellness', name: 'Wellness Center', color: 0x66BB6A }, // Bright green
            { type: 'coworking', name: 'Co-working Space', color: 0xFFA726 } // Bright orange
        ];
        
        for (let i = 0; i < count && this.addedBusinesses.length < businessPositions.length; i++) {
            const pos = businessPositions[this.addedBusinesses.length];
            const businessData = businessTypes[this.addedBusinesses.length % businessTypes.length];
            const business = this.createNewBusiness(pos[0], pos[1], businessData);
            this.addedBusinesses.push({ business, position: pos, type: businessData.type });
        }
    }
    
    createNewBusiness(x, z, businessData) {
        const group = new THREE.Group();
        
        // Business building - slightly larger than houses
        const buildingGeometry = new THREE.BoxGeometry(2.2, 2.5, 2.2);
        const buildingMaterial = new THREE.MeshLambertMaterial({ color: businessData.color });
        const building = new THREE.Mesh(buildingGeometry, buildingMaterial);
        building.position.y = 1.25;
        building.castShadow = true;
        group.add(building);
        
        // Flat commercial roof with cozy colors
        const roofGeometry = new THREE.BoxGeometry(2.3, 0.2, 2.3);
        const roofColors = [0x8B4513, 0xA0522D, 0xCD853F, 0xD2691E, 0xBC8F8F];
        const roofColor = roofColors[Math.floor(Math.random() * roofColors.length)];
        const roofMaterial = new THREE.MeshLambertMaterial({ color: roofColor });
        const roof = new THREE.Mesh(roofGeometry, roofMaterial);
        roof.position.y = 2.6;
        roof.castShadow = true;
        group.add(roof);
        
        // Simple business sign
        const signGeometry = new THREE.BoxGeometry(1.8, 0.3, 0.1);
        const signMaterial = new THREE.MeshLambertMaterial({ color: 0xFFFFFF });
        const sign = new THREE.Mesh(signGeometry, signMaterial);
        sign.position.y = 2.0;
        sign.position.z = 1.15;
        group.add(sign);
        
        group.position.set(x, 0, z);
        this.scene.add(group);
        
        return { group, name: businessData.name, type: businessData.type };
    }
    
    addCommunityGarden() {
        // Find a good central location for the community garden
        const gardenPosition = [-4, 4]; // Nice central spot
        const garden = this.createCommunityGarden(gardenPosition[0], gardenPosition[1]);
        this.addedCommunitySpaces.push({ element: garden, type: 'garden', position: gardenPosition });
    }
    
    createCommunityGarden(x, z) {
        const group = new THREE.Group();
        
        // Garden plots (raised beds)
        for (let i = 0; i < 6; i++) {
            const plotGeometry = new THREE.BoxGeometry(1.5, 0.2, 0.8);
            const plotMaterial = new THREE.MeshLambertMaterial({ color: 0x8B4513 });
            const plot = new THREE.Mesh(plotGeometry, plotMaterial);
            
            const plotX = (i % 3 - 1) * 2;
            const plotZ = Math.floor(i / 3) * 1.5 - 0.75;
            plot.position.set(plotX, 0.1, plotZ);
            group.add(plot);
            
            // Plants in each plot
            for (let j = 0; j < 3; j++) {
                const plantGeometry = new THREE.SphereGeometry(0.1, 6, 6);
                const plantColors = [0x2ECC71, 0x27AE60, 0x16A085];
                const plantMaterial = new THREE.MeshLambertMaterial({ color: plantColors[j % 3] });
                const plant = new THREE.Mesh(plantGeometry, plantMaterial);
                plant.position.set(plotX + (j - 1) * 0.4, 0.3, plotZ);
                group.add(plant);
            }
        }
        
        // Garden shed
        const shedGeometry = new THREE.BoxGeometry(1.2, 1.5, 1.0);
        const shedMaterial = new THREE.MeshLambertMaterial({ color: 0x8B4513 });
        const shed = new THREE.Mesh(shedGeometry, shedMaterial);
        shed.position.set(3, 0.75, 0);
        group.add(shed);
        
        group.position.set(x, 0, z);
        this.scene.add(group);
        
        return group;
    }
    
    addPublicPlaza() {
        // Central plaza location
        const plazaPosition = [0, 0]; // Dead center of neighborhood
        const plaza = this.createPublicPlaza(plazaPosition[0], plazaPosition[1]);
        this.addedCommunitySpaces.push({ element: plaza, type: 'plaza', position: plazaPosition });
    }
    
    createPublicPlaza(x, z) {
        const group = new THREE.Group();
        
        // Plaza base - circular paved area
        const plazaGeometry = new THREE.CylinderGeometry(4, 4, 0.1, 16);
        const plazaMaterial = new THREE.MeshLambertMaterial({ color: 0xBDC3C7 });
        const plaza = new THREE.Mesh(plazaGeometry, plazaMaterial);
        plaza.position.y = 0.05;
        group.add(plaza);
        
        // Central fountain
        const fountainBaseGeometry = new THREE.CylinderGeometry(1, 1, 0.5, 12);
        const fountainMaterial = new THREE.MeshLambertMaterial({ color: 0x85929E });
        const fountainBase = new THREE.Mesh(fountainBaseGeometry, fountainMaterial);
        fountainBase.position.y = 0.35;
        group.add(fountainBase);
        
        const fountainTopGeometry = new THREE.CylinderGeometry(0.3, 0.3, 0.8, 8);
        const fountainTop = new THREE.Mesh(fountainTopGeometry, fountainMaterial);
        fountainTop.position.y = 0.9;
        group.add(fountainTop);
        
        // Surrounding benches in a circle
        for (let i = 0; i < 6; i++) {
            const angle = (i / 6) * Math.PI * 2;
            const benchX = Math.cos(angle) * 3;
            const benchZ = Math.sin(angle) * 3;
            
            const benchGroup = new THREE.Group();
            
            // Bench
            const seatGeometry = new THREE.BoxGeometry(1.2, 0.1, 0.4);
            const seatMaterial = new THREE.MeshLambertMaterial({ color: 0x8B4513 });
            const seat = new THREE.Mesh(seatGeometry, seatMaterial);
            seat.position.y = 0.3;
            benchGroup.add(seat);
            
            benchGroup.position.set(benchX, 0, benchZ);
            benchGroup.rotation.y = angle + Math.PI/2; // Face the fountain
            group.add(benchGroup);
        }
        
        // Decorative lampposts
        for (let i = 0; i < 4; i++) {
            const angle = (i / 4) * Math.PI * 2 + Math.PI/4;
            const lampX = Math.cos(angle) * 3.5;
            const lampZ = Math.sin(angle) * 3.5;
            
            const lampGeometry = new THREE.CylinderGeometry(0.05, 0.05, 2.5);
            const lampMaterial = new THREE.MeshLambertMaterial({ color: 0x2C3E50 });
            const lamp = new THREE.Mesh(lampGeometry, lampMaterial);
            lamp.position.set(lampX, 1.25, lampZ);
            group.add(lamp);
            
            // Lamp light
            const lightGeometry = new THREE.SphereGeometry(0.2, 8, 8);
            const lightMaterial = new THREE.MeshLambertMaterial({ color: 0xF1C40F });
            const light = new THREE.Mesh(lightGeometry, lightMaterial);
            light.position.set(lampX, 2.3, lampZ);
            group.add(light);
        }
        
        group.position.set(x, 0, z);
        this.scene.add(group);
        
        return group;
    }
    
    createBench(x, z, rotation = 0) {
        const group = new THREE.Group();
        
        // Bench seat
        const seatGeometry = new THREE.BoxGeometry(1.2, 0.1, 0.4);
        const seatMaterial = new THREE.MeshLambertMaterial({ color: 0x8B4513 });
        const seat = new THREE.Mesh(seatGeometry, seatMaterial);
        seat.position.y = 0.3;
        group.add(seat);
        
        // Bench back
        const backGeometry = new THREE.BoxGeometry(1.2, 0.5, 0.1);
        const back = new THREE.Mesh(backGeometry, seatMaterial);
        back.position.y = 0.55;
        back.position.z = -0.15;
        group.add(back);
        
        // Bench legs
        for (let i = 0; i < 4; i++) {
            const legGeometry = new THREE.BoxGeometry(0.1, 0.3, 0.1);
            const leg = new THREE.Mesh(legGeometry, seatMaterial);
            leg.position.x = (i % 2 === 0) ? -0.5 : 0.5;
            leg.position.z = (i < 2) ? 0.15 : -0.15;
            leg.position.y = 0.15;
            group.add(leg);
        }
        
        group.position.set(x, 0, z);
        group.rotation.y = rotation;
        this.scene.add(group);
        
        return group;
    }
    
    createBasketballCourt(x, z) {
        const group = new THREE.Group();
        
        // Court surface
        const courtGeometry = new THREE.PlaneGeometry(6, 4);
        const courtMaterial = new THREE.MeshLambertMaterial({ color: 0x8B4513 });
        const court = new THREE.Mesh(courtGeometry, courtMaterial);
        court.rotation.x = -Math.PI / 2;
        court.position.y = 0.01;
        group.add(court);
        
        // Court lines
        const lineMaterial = new THREE.MeshLambertMaterial({ color: 0xFFFFFF });
        
        // Center line
        const centerLineGeometry = new THREE.PlaneGeometry(0.1, 4);
        const centerLine = new THREE.Mesh(centerLineGeometry, lineMaterial);
        centerLine.rotation.x = -Math.PI / 2;
        centerLine.position.y = 0.02;
        group.add(centerLine);
        
        // Basketball hoops
        for (let i = 0; i < 2; i++) {
            const hoopGroup = new THREE.Group();
            
            // Hoop pole
            const poleGeometry = new THREE.CylinderGeometry(0.1, 0.1, 2.5);
            const poleMaterial = new THREE.MeshLambertMaterial({ color: 0x666666 });
            const pole = new THREE.Mesh(poleGeometry, poleMaterial);
            pole.position.y = 1.25;
            hoopGroup.add(pole);
            
            // Hoop rim
            const rimGeometry = new THREE.TorusGeometry(0.3, 0.05, 8, 16);
            const rimMaterial = new THREE.MeshLambertMaterial({ color: 0xFF6600 });
            const rim = new THREE.Mesh(rimGeometry, rimMaterial);
            rim.position.y = 2.3;
            rim.rotation.x = Math.PI / 2;
            hoopGroup.add(rim);
            
            hoopGroup.position.x = i === 0 ? -2.8 : 2.8;
            group.add(hoopGroup);
        }
        
        group.position.set(x, 0, z);
        this.scene.add(group);
        
        return group;
    }
    
    createSculpture(x, z, type) {
        const group = new THREE.Group();
        
        // Sculpture base
        const baseGeometry = new THREE.CylinderGeometry(0.8, 0.8, 0.2);
        const baseMaterial = new THREE.MeshLambertMaterial({ color: 0x999999 });
        const base = new THREE.Mesh(baseGeometry, baseMaterial);
        base.position.y = 0.1;
        group.add(base);
        
        // Different sculpture types
        const sculptureColors = [0x2E86AB, 0xA23B72, 0xF18F01, 0xC73E1D, 0x592E83];
        const sculptureColor = sculptureColors[type % sculptureColors.length];
        const sculptureMaterial = new THREE.MeshLambertMaterial({ color: sculptureColor });
        
        if (type === 0) {
            // Abstract tall sculpture
            const sculptureGeometry = new THREE.BoxGeometry(0.3, 1.5, 0.3);
            const sculpture = new THREE.Mesh(sculptureGeometry, sculptureMaterial);
            sculpture.position.y = 1;
            sculpture.rotation.y = Math.PI / 4;
            group.add(sculpture);
        } else if (type === 1) {
            // Sphere sculpture
            const sculptureGeometry = new THREE.SphereGeometry(0.4, 8, 8);
            const sculpture = new THREE.Mesh(sculptureGeometry, sculptureMaterial);
            sculpture.position.y = 0.6;
            group.add(sculpture);
        } else {
            // Abstract twisted sculpture
            const sculptureGeometry = new THREE.ConeGeometry(0.4, 1.2, 6);
            const sculpture = new THREE.Mesh(sculptureGeometry, sculptureMaterial);
            sculpture.position.y = 0.8;
            sculpture.rotation.z = Math.PI / 6;
            group.add(sculpture);
        }
        
        group.position.set(x, 0, z);
        this.scene.add(group);
        
        return group;
    }
    
    addPublicPark() {
        // Define two distinct park locations
        const parkLocations = [
            { 
                center: { x: -15, z: -15 }, 
                name: 'West Park',
                elements: [
                    { type: 'trees', positions: [{ x: -17, z: -17 }, { x: -13, z: -17 }, { x: -15, z: -13 }] },
                    { type: 'benches', positions: [{ x: -15, z: -15, rotation: 0 }, { x: -17, z: -13, rotation: Math.PI/4 }] },
                    { type: 'flowers', positions: [{ x: -16, z: -14 }, { x: -14, z: -16 }, { x: -13, z: -14 }] },
                    { type: 'sculpture', position: { x: -15, z: -17 } }
                ]
            },
            { 
                center: { x: 15, z: 15 }, 
                name: 'East Park',
                elements: [
                    { type: 'trees', positions: [{ x: 17, z: 17 }, { x: 13, z: 17 }, { x: 15, z: 13 }] },
                    { type: 'benches', positions: [{ x: 15, z: 15, rotation: Math.PI }, { x: 17, z: 13, rotation: -Math.PI/4 }] },
                    { type: 'flowers', positions: [{ x: 16, z: 14 }, { x: 14, z: 16 }, { x: 13, z: 14 }, { x: 17, z: 15 }] },
                    { type: 'basketball', position: { x: 15, z: 17 } }
                ]
            }
        ];
        
        // Determine which park to add based on current park count
        const currentParkCount = this.addedParks.filter(p => p.type === 'park').length;
        if (currentParkCount >= 2) return; // Max 2 parks
        
        const parkToAdd = parkLocations[currentParkCount];
        const parkGroup = new THREE.Group();
        
        // Apply current investment scale
        const maxScaleInvestment = 10000000;
        const investmentProgress = Math.min(this.neighborhoodMoney / maxScaleInvestment, 1.0);
        const parkScale = 1.0 + (investmentProgress * 0.10);
        
        // Store park elements for scaling
        const parkElements = [];
        
        // Add all park elements
        parkToAdd.elements.forEach(elementGroup => {
            switch (elementGroup.type) {
                case 'trees':
                    elementGroup.positions.forEach(pos => {
                        this.createTree(pos.x, pos.z); // Trees add themselves to scene
                        // Find the just-added tree in the scene for scaling
                        const addedTree = this.scene.children[this.scene.children.length - 1];
                        addedTree.scale.set(parkScale, parkScale, parkScale);
                        parkElements.push(addedTree);
                    });
                    break;
                    
                case 'benches':
                    elementGroup.positions.forEach(pos => {
                        const bench = this.createBench(pos.x, pos.z, pos.rotation || 0);
                        bench.scale.set(parkScale, parkScale, parkScale);
                        this.addedParks.push({ type: 'bench', element: bench });
                        parkElements.push(bench);
                    });
                    break;
                    
                case 'flowers':
                    elementGroup.positions.forEach(pos => {
                        this.createFlower(pos.x, pos.z); // Flowers are added directly to scene
                    });
                    break;
                    
                case 'sculpture':
                    const sculpture = this.createSculpture(elementGroup.position.x, elementGroup.position.z, currentParkCount);
                    sculpture.scale.set(parkScale, parkScale, parkScale);
                    this.addedParks.push({ type: 'sculpture', element: sculpture });
                    parkElements.push(sculpture);
                    break;
                    
                case 'basketball':
                    const court = this.createBasketballCourt(elementGroup.position.x, elementGroup.position.z);
                    court.scale.set(parkScale, parkScale, parkScale);
                    this.addedParks.push({ type: 'basketball', element: court });
                    parkElements.push(court);
                    break;
            }
        });
        
        // Track that we've added a park with its elements
        this.addedParks.push({ type: 'park', name: parkToAdd.name, elements: parkElements });
    }
    
    addParkElements(benchCount, hasBasketballCourt, sculptureCount) {
        // Legacy function for individual park elements
        const benchPositions = [
            { x: -12, z: -18, rotation: 0 },
            { x: 12, z: -18, rotation: Math.PI },
            { x: -18, z: -12, rotation: Math.PI / 2 },
            { x: 18, z: -12, rotation: -Math.PI / 2 },
        ];
        
        const maxScaleInvestment = 10000000;
        const investmentProgress = Math.min(this.neighborhoodMoney / maxScaleInvestment, 1.0);
        const parkScale = 1.0 + (investmentProgress * 0.10);
        
        // Add individual benches
        for (let i = 0; i < benchCount && i < benchPositions.length; i++) {
            const pos = benchPositions[i];
            const bench = this.createBench(pos.x, pos.z, pos.rotation);
            bench.scale.set(parkScale, parkScale, parkScale);
            this.addedParks.push({ type: 'bench', element: bench });
        }
        
        // Add basketball court
        if (hasBasketballCourt && !this.addedParks.some(p => p.type === 'basketball')) {
            const court = this.createBasketballCourt(0, -12);
            court.scale.set(parkScale, parkScale, parkScale);
            this.addedParks.push({ type: 'basketball', element: court });
        }
        
        // Add sculptures
        const sculpturePositions = [
            { x: -8, z: -8 }, { x: 8, z: -8 }
        ];
        
        for (let i = 0; i < sculptureCount && i < sculpturePositions.length; i++) {
            const pos = sculpturePositions[i];
            const sculpture = this.createSculpture(pos.x, pos.z, i);
            sculpture.scale.set(parkScale, parkScale, parkScale);
            this.addedParks.push({ type: 'sculpture', element: sculpture });
        }
    }
    
    createFlower(x, z) {
        const group = new THREE.Group();
        
        // Flower stem
        const stemGeometry = new THREE.CylinderGeometry(0.02, 0.02, 0.3);
        const stemMaterial = new THREE.MeshLambertMaterial({ color: 0x228B22 });
        const stem = new THREE.Mesh(stemGeometry, stemMaterial);
        stem.position.y = 0.15;
        group.add(stem);
        
        // Flower petals
        const petalColors = [0xFF69B4, 0xFF1493, 0xFFB6C1, 0xFF6347, 0xFFA500];
        const petalGeometry = new THREE.SphereGeometry(0.08, 6, 6);
        const petalMaterial = new THREE.MeshLambertMaterial({ 
            color: petalColors[Math.floor(Math.random() * petalColors.length)] 
        });
        const petals = new THREE.Mesh(petalGeometry, petalMaterial);
        petals.position.y = 0.35;
        group.add(petals);
        
        group.position.set(x, 0, z);
        this.scene.add(group);
    }
    
    addNewHouses(count) {
        const emptySpots = [
            // CONTAINED within street grid boundaries (-18 to 18)
            // EXCLUDE center area (-7 to +7) to keep community spaces clear
            // Core positions around existing blocks
            [-18, -6], [18, -6], [-6, -18], [6, -18],
            [-18, 6], [18, 6], [-6, 18], [6, 18],
            [-14, -14], [14, -14], [-14, 14], [14, 14],
            // Fill within each city block (avoiding center)
            [-10, -6], [10, -6], [-10, 6], [10, 6],
            [-2, -14], [2, -14], [-14, -2], [14, -2],
            [-2, 14], [2, 14], [-14, 2], [14, 2],
            [-16, 0], [16, 0], [0, -16], [0, 16],
            [-8, -8], [8, -8], [-8, 8], [8, 8],
            // Dense infill within blocks (avoiding center)
            [-12, 0], [12, 0], [0, -12], [0, 12],
            // Fill all remaining block spaces
            [-18, -10], [18, -10], [-10, -18], [10, -18],
            [-18, 10], [18, 10], [-10, 18], [10, 18],
            [-16, -8], [16, -8], [-8, -16], [8, -16],
            [-16, 8], [16, 8], [-8, 16], [8, 16],
            [-18, -2], [18, -2], [-2, -18], [2, -18],
            [-18, 2], [18, 2], [-2, 18], [2, 18],
            // Maximum density within street boundaries (avoiding center)
            [-12, -6], [12, -6], [-12, 6], [12, 6],
            [-16, -4], [16, -4], [-16, 4], [16, 4],
            [-14, -6], [14, -6], [-14, 6], [14, 6],
            [-18, -14], [18, -14], [-14, -18], [14, -18],
            [-18, 14], [18, 14], [-14, 18], [14, 18],
            // Fill corners of blocks (avoiding center)
            [-10, -2], [10, -2], [-10, 2], [10, 2],
            [-8, -2], [8, -2], [-8, 2], [8, 2],
            [-12, -4], [12, -4], [-12, 4], [12, 4],
            // Additional dense positions
            [-18, -8], [18, -8], [-8, -18], [8, -18],
            [-18, 8], [18, 8], [-8, 18], [8, 18],
            [-16, -6], [16, -6], [-16, 6], [16, 6],
            [-14, -8], [14, -8], [-8, -14], [8, -14],
            [-14, 8], [14, 8], [-8, 14], [8, 14],
            [-10, -4], [10, -4], [-10, 4], [10, 4],
            [-12, -2], [12, -2], [-12, 2], [12, 2],
            // Final density within boundaries (avoiding center)
            [-16, -2], [16, -2], [-16, 2], [16, 2],
            [-14, -4], [14, -4], [-14, 4], [14, 4],
            [-18, -4], [18, -4], [-18, 4], [18, 4]
        ];
        
        for (let i = 0; i < count && this.addedHouses.length < emptySpots.length; i++) {
            const pos = emptySpots[this.addedHouses.length];
            const house = this.createHouse(pos[0], pos[1], this.houses.length + this.addedHouses.length);
            
            // Apply current investment scale to new house
            const intensity = this.redirectionRate;
            const scale = 1.0 + (intensity * 0.15); // Subtle scaling
            house.group.scale.set(scale, scale, scale);
            
            this.addedHouses.push({ house, position: pos });
            
            // Add money streams for new houses
            const randomCorp = this.corporateBuildings[Math.floor(Math.random() * this.corporateBuildings.length)];
            const corpStream = this.createMoneyStream(
                { x: pos[0], z: pos[1] },
                randomCorp.position,
                0x2ecc71
            );
            this.moneyStreams.push(corpStream);
            
            // Add local stream too
            const nearestStore = this.localStores[this.houses.length % this.localStores.length];
            const localStream = this.createMoneyStream(
                { x: pos[0], z: pos[1] },
                nearestStore.position,
                0x2ecc71,
                true
            );
            this.moneyStreams.push(localStream);
            
            // Add to main houses array for money tracking
            this.houses.push({ group: house.group, position: { x: pos[0], z: pos[1] }, moneyGeneration: Math.random() * 0.5 + 0.3 });
        }
    }
    

    
    updateBuildingScales() {
        // Scale buildings based on cumulative neighborhood investment
        const maxScaleInvestment = 10000000; // $10M for max building scaling
        const investmentProgress = Math.min(this.neighborhoodMoney / maxScaleInvestment, 1.0);
        
        // Subtle scale factor: 1.0 (no investment) to 1.15 (full investment)
        const baseScale = 1.0;
        const maxGrowth = 0.15; // Much more subtle growth
        const scale = baseScale + (investmentProgress * maxGrowth);
        
        // Scale original houses
        this.houses.forEach(house => {
            if (house.group) {
                house.group.scale.set(scale, scale, scale);
            }
        });
        
        // Scale local stores (they grow slightly more since they receive direct investment)
        this.localStores.forEach(store => {
            const storeScale = baseScale + (investmentProgress * 0.20); // 20% max growth for stores
            store.group.scale.set(storeScale, storeScale, storeScale);
        });
        
        // Scale newly added houses from flourishing
        this.addedHouses.forEach(houseData => {
            if (houseData.house && houseData.house.group) {
                houseData.house.group.scale.set(scale, scale, scale);
            }
        });
        
        // Scale park elements (benches, basketball court, sculptures)
        this.addedParks.forEach(parkData => {
            if (parkData.element) {
                const parkScale = baseScale + (investmentProgress * 0.10); // 10% max growth for park elements
                parkData.element.scale.set(parkScale, parkScale, parkScale);
            }
        });
        
        // Scale new businesses (they benefit most from local investment)
        this.addedBusinesses.forEach(businessData => {
            if (businessData.business && businessData.business.group) {
                const businessScale = baseScale + (investmentProgress * 0.25); // 25% max growth for businesses
                businessData.business.group.scale.set(businessScale, businessScale, businessScale);
            }
        });
        
        // Scale community spaces
        this.addedCommunitySpaces.forEach(spaceData => {
            if (spaceData.element) {
                const spaceScale = baseScale + (investmentProgress * 0.15); // 15% max growth for community spaces
                spaceData.element.scale.set(spaceScale, spaceScale, spaceScale);
            }
        });
    }
    
    updateGrassHealth() {
        if (!this.ground) return;
        
        // Transition from muted olive grass to royal healthy grass green
        const unhealthyColor = { r: 141, g: 179, b: 96 };  // Muted olive starting grass
        const healthyColor = { r: 34, g: 139, b: 34 };     // Royal healthy grass green
        
        // Base health on both current flow (40%) and cumulative investment (60%) - favor investment more
        const currentFlowIntensity = this.redirectionRate;
        const maxInvestmentForGrass = 3000000; // $3M for max grass health - much sooner!
        const investmentIntensity = Math.min(this.neighborhoodMoney / maxInvestmentForGrass, 1.0);
        const combinedIntensity = (currentFlowIntensity * 0.4) + (investmentIntensity * 0.6);
        
        const r = Math.round(unhealthyColor.r + (healthyColor.r - unhealthyColor.r) * combinedIntensity);
        const g = Math.round(unhealthyColor.g + (healthyColor.g - unhealthyColor.g) * combinedIntensity);
        const b = Math.round(unhealthyColor.b + (healthyColor.b - unhealthyColor.b) * combinedIntensity);
        
        // Convert to hex and update ground color
        const newColor = (r << 16) | (g << 8) | b;
        this.ground.material.color.setHex(newColor);
    }
    
    updateNeighborhoodFlourishing() {
        // Base flourishing on cumulative neighborhood investment with $10M max
        const maxFlourishMoney = 10000000; // $10 million for full flourishing
        const flourishingProgress = Math.min(this.neighborhoodMoney / maxFlourishMoney, 1.0);
        const targetLevel = Math.floor(flourishingProgress * 4); // 0-4 levels based on total money
        
        if (targetLevel > this.flourishingLevel) {
            // Add flourishing elements progressively - focus on businesses, parks, and community!
            if (this.flourishingLevel === 0 && targetLevel >= 1) {
                // Level 1: $2.5M+ invested - Basic neighborhood growth
                this.addNewHouses(12); // Fewer houses
                this.addNewBusinesses(3); // New local businesses!
                this.addFlourishingTrees(15);
                this.addFlowers(18);
            }
            if (this.flourishingLevel <= 1 && targetLevel >= 2) {
                // Level 2: $5M+ invested - Community spaces emerge
                this.addNewHouses(8); // Even fewer houses
                this.addNewBusinesses(4); // More businesses
                this.addPublicPark(); // West Park with trees, benches, flowers, sculpture
                this.addFlowers(24);
                this.addFlourishingTrees(9);
            }
            if (this.flourishingLevel <= 2 && targetLevel >= 3) {
                // Level 3: $7.5M+ invested - Recreation and culture
                this.addNewHouses(6); // Minimal new housing
                this.addNewBusinesses(3); // Cultural businesses
                this.addPublicPark(); // East Park with trees, benches, flowers, basketball court
                this.addCommunityGarden(); // Community garden!
                this.addFlourishingTrees(12);
                this.addFlowers(30);
            }
            if (this.flourishingLevel <= 3 && targetLevel >= 4) {
                // Level 4: $10M+ invested - Full community flourishing!
                this.addNewHouses(4); // Very few new houses
                this.addNewBusinesses(5); // Premium businesses
                this.addPublicPlaza(); // Central community plaza
                this.addFlourishingTrees(24);
                this.addFlowers(36);
                this.addParkElements(8, true, 8); // More varied park elements
            }
            
            this.flourishingLevel = targetLevel;
        }
        // Note: Flourishing doesn't go backwards since it's based on cumulative investment
        
        // Update grass health based on current money flow rate AND total investment
        this.updateGrassHealth();
    }
    
    advanceMonth() {
        this.currentMonth++;
        if (this.currentMonth > 12) {
            this.currentMonth = 1;
            this.currentYear++;
        }
    }
    
    updateTimeDisplay() {
        if (this.currentTimeSpan) {
            const monthName = this.monthNames[this.currentMonth - 1];
            this.currentTimeSpan.textContent = `${monthName} ${this.currentYear}`;
        }
    }
    
    updateMoneyDisplay() {
        // Format money amounts for display (no K shortening)
        const formatMoney = (amount) => {
            if (amount >= 1000000) {
                return '$' + (amount / 1000000).toFixed(1) + 'M';
            } else {
                return '$' + Math.round(amount).toLocaleString();
            }
        };
        
        this.moneyOutSpan.textContent = formatMoney(this.corporateMoney);
        this.moneyInSpan.textContent = formatMoney(this.neighborhoodMoney);
    }
    
    updateRedirection() {
        // Update money flow visualization - now showing cumulative amounts
        this.updateMoneyDisplay();
        
        // Scale all buildings based on neighborhood investment
        this.updateBuildingScales();
        
        // Add flourishing effects when money comes into neighborhood
        this.updateNeighborhoodFlourishing();
        
        // Visual effects are now handled by updateStreamVisibility
    }
    
    createLocalStreams() {
        // Create local streams for ALL houses to match corporate streams
        this.houses.forEach((house, index) => {
            const nearestStore = this.localStores[index % this.localStores.length];
            const localStream = this.createMoneyStream(
                house.position,
                nearestStore.position,
                0x2ecc71, // Green for money staying local
                true // Mark as local stream
            );
            this.moneyStreams.push(localStream);
        });
    }
    
    clearLocalStreams() {
        // Remove local streams, keep only corporate streams
        this.moneyStreams = this.moneyStreams.filter(stream => {
            if (stream.isLocal) { // Local streams
                // Remove particles from scene
                stream.particles.forEach(particle => {
                    this.scene.remove(particle);
                });
                return false; // Remove from array
            }
            return true; // Keep corporate streams
        });
    }
    
    updateStreamVisibility() {
        // Group streams by house to ensure 1:1 replacement
        const corporateStreams = this.moneyStreams.filter(s => !s.isLocal);
        const localStreams = this.moneyStreams.filter(s => s.isLocal);
        
        const totalHouses = this.houses.length;
        const localCount = Math.floor(totalHouses * this.redirectionRate);
        
        // Show corporate streams for houses that aren't redirected
        corporateStreams.forEach((stream, index) => {
            const shouldShow = index >= localCount;
            stream.particles.forEach(particle => {
                particle.visible = shouldShow;
            });
        });
        
        // Show local streams for houses that are redirected
        localStreams.forEach((stream, index) => {
            const shouldShow = index < localCount;
            stream.particles.forEach(particle => {
                particle.visible = shouldShow;
            });
        });
    }
    
    animate() {
        requestAnimationFrame(() => this.animate());
        
        this.updateMoneyStreams();
        this.updateMoneyAccumulation();
        
        // Fixed camera position - no rotation
        this.renderer.render(this.scene, this.camera);
    }
    
    /* The scene was composed for a roughly 3:2 box. Now that the container
       bleeds to the full viewport, a fixed camera distance leaves the
       neighbourhood exactly the size it always was while the extra width
       turns into empty sky -- so pull the camera in as the box widens.
       Scaling the position vector, rather than moving the camera, keeps the
       isometric angle identical; only the distance changes.

       It compensates partway and is clamped, on purpose. A true width-fit
       needs about 2.5x at ultrawide, and because the frame's height shrinks
       at exactly the rate its width grows, that crops the plate top and
       bottom. The exponent takes most of the visible gain before that bites.
       Below the reference aspect the multiplier is exactly 1, so a phone and
       anything inside the old reading column are pixel-for-pixel unchanged. */
    fitCamera() {
        const REFERENCE_ASPECT = 1.55;
        const aspect = this.container.offsetWidth / this.container.offsetHeight;
        const zoom = Math.pow(REFERENCE_ASPECT / Math.max(aspect, REFERENCE_ASPECT), 0.25);
        const k = Math.max(0.85, zoom);
        this.camera.position.set(25 * k, 30 * k, 25 * k);
        /* Aim slightly below the origin as we zoom in. Zooming alone ran the
           plate's bottom vertex off the bottom edge while a band of empty
           sky sat above the towers doing nothing; looking lower lifts the
           whole composition into that band, which is what pays for the
           extra scale.

           Both numbers are held back from what looks best at a single
           width, because the two failures are at opposite ends: too much
           zoom runs the plate off the bottom, and too much lift clips the
           CORPORATIONS label off the top. Past about 3.5:1 the frame is too
           short to hold both at full strength, so the ultrawide case keeps
           its side sky rather than losing content -- a square isometric
           plate cannot fill a 3.7:1 frame, and that is geometry, not a bug. */
        this.camera.lookAt(0, -10 * (1 - k), 0);
    }

    onWindowResize() {
        this.camera.aspect = this.container.offsetWidth / this.container.offsetHeight;
        this.fitCamera();
        this.camera.updateProjectionMatrix();
        this.renderer.setSize(this.container.offsetWidth, this.container.offsetHeight);
    }
}

// Initialize the visualization when the page loads
document.addEventListener('DOMContentLoaded', () => {
    new NeighborhoodVisualization();
});
</script>]]></content:encoded>
            <media:content url="https://cloudy.nyc/og-card.jpg" medium="image"/>
            <media:thumbnail url="https://cloudy.nyc/og-card.jpg"/>
        </item>
        <item>
            <title>Working with Electron</title>
            <pubDate>Sun, 12 Nov 2017 00:00:00 +0000</pubDate>
            <author>Peter Valdez</author>
            <link>https://cloudy.nyc/blog/working-with-electron/</link>
            <guid>https://cloudy.nyc/blog/working-with-electron/</guid>
            <description>Working with Electron is a pleasure. I just built an app called Redub with it. For the uninitiated, here is how the creators describe Electron:</description>
            <content:encoded xml:base="https://cloudy.nyc/blog/working-with-electron/"><![CDATA[<p>Working with <a href="https://electron.atom.io/">Electron</a> is a pleasure. I just built an
app called <a href="/gone/redub-audio/">Redub</a> with it. For the uninitiated, here is how the
creators describe Electron:</p>
<blockquote>
<p>Electron is a framework for creating native applications with web technologies like JavaScript,
HTML, and CSS.</p>
</blockquote>
<p>Most programmers know how to build web apps. Now, with that same knowledge, you can build
cross-platform desktop apps.</p>
<p>Electron gets a
<a href="https://josephg.com/blog/electron-is-flash-for-the-desktop/">lot</a>
<a href="https://medium.com/@caspervonb/electron-is-cancer-b066108e6c32">of</a>
<a href="https://drewdevault.com/2016/11/24/Electron-considered-harmful.html">crap</a>
for being too resource intensive and for enabling lazy developers. The haters say that better
frameworks for desktop apps already exist and that you should use those instead.</p>
<p>They're right, Electron apps are more intensive than I'd like. For those unaware, it's because
every app has a full browser baked into it. That's how you're able to use Javascript, HTML, and CSS.</p>
<p>But let me be the first to say: the haters are missing the point, as usual. The real value of
Electron is that it lets you move from <strong>random idea</strong> to <strong>cross-platform app</strong> fast, using the
web dev knowledge you probably already have (which also means you have a huge community to draw
from).</p>
<p>Also, it's not like you the performance problems can't be addressed. Solutions are already being
proposed. For example, there's talks of an <a href="https://github.com/electron/electron/issues/673">"Electron runtime"</a>.
Basically, every Electron app on your system would share a single copy of chromium.</p>
<p>Finally, let's be real. Most people don't have the time to learn a new language and cross-platform
framework. So if there wasn't a way to use web tech to build desktop apps, some ideas wouldn't ever
get created.</p>
<p>So, I ask you, dear reader: would you rather live in a world with apps that are memory hogs but
fulfill unique needs, or not have those apps exist at all? I'm definitely with the first.</p>
]]></content:encoded>
            <media:content url="https://cloudy.nyc/og-card.jpg" medium="image"/>
            <media:thumbnail url="https://cloudy.nyc/og-card.jpg"/>
        </item>
        <item>
            <title>Insert an audio clip to a video file with FFMPEG</title>
            <pubDate>Thu, 26 Oct 2017 00:00:00 +0000</pubDate>
            <author>Peter Valdez</author>
            <link>https://cloudy.nyc/blog/splice-audio/</link>
            <guid>https://cloudy.nyc/blog/splice-audio/</guid>
            <description>For a hobby project of mine, I wanted to be able to merge an audio clip into the existing audio track of a video file.</description>
            <content:encoded xml:base="https://cloudy.nyc/blog/splice-audio/"><![CDATA[<h3 id="the-goal">The goal</h3>
<p>For a <a href="https://github.com/azlyth/redub">hobby project of mine</a>, I wanted to be able
to merge an audio clip into the existing audio track of a video file.</p>
<h3 id="the-command">The command</h3>
<p>After struggling for an hour or two, I finally came up with the correct incantation of the command
<code>ffmpeg</code> to overlay an audio clip to a video file:</p>
<pre data-lang="bash" class="language-bash "><code class="language-bash" data-lang="bash">ffmpeg \
  -i movie.mkv \
  -i new-audio-clip.mp3 \
  -filter_complex &quot;[1:0] adelay=2728000|2728000 [delayed];[0:1][delayed] amix=inputs=2&quot; \
  -map 0:0 \
  -c:a aac -strict -2 \
  -c:v copy \
  output.mp4
</code></pre>
<h3 id="the-explanation">The explanation</h3>
<p>Line by line:</p>
<ul>
<li><strong>-i movie.mkv</strong>: set the video/audio file as an input</li>
<li><strong>-i new-audio-clip.mp3</strong>: set the audio clip as an input</li>
<li><strong>-filter_complex "..."</strong>: specify the two following filters:
<ul>
<li><strong>"[1:0] adelay=2728000|2728000 [delayed]</strong>: the adelay filter, specifically:
<ul>
<li><strong>[1:0]</strong>: using the second input file's first stream</li>
<li><strong>adelay=2728000|2728000</strong>: add a delay of 2,728,000 milliseconds to both channels of the audio (the left and right channels, because the audio is stereo)</li>
<li><strong>[delayed]</strong>: assign the name "delayed" to the new stream</li>
</ul>
</li>
<li><strong>[0:1][delayed] amix=inputs=2</strong>: the amix filter, specifically:
<ul>
<li><strong>[0:1][delayed]</strong>: using the first input's second stream and the "delayed" stream</li>
<li><strong>amix=inputs=2</strong>: mix the two audio streams (NOTE: I'm not 100% on the meaning of <strong>inputs=2</strong> here)</li>
</ul>
</li>
</ul>
</li>
<li><strong>-map 0:0</strong>: add the first input's first stream to the output (which is the video)</li>
<li><strong>-c:a aac -strict -2</strong>: use AAC for audio encoding</li>
<li><strong>-c:v copy</strong>: don't re-encode the video (in other words, copy the original encoding)</li>
<li><strong>output.mp4</strong>: specify the output file</li>
</ul>
<h3 id="js-version">JS version</h3>
<p>And for any of you interested in doing this in JavaScript, here's the same command translated to
<a href="https://github.com/fluent-ffmpeg/node-fluent-ffmpeg">node-fluent-ffmpeg</a>:</p>
<pre data-lang="js" class="language-js "><code class="language-js" data-lang="js">ffmpeg()
  .input(&#x27;movie.mkv&#x27;)
  .input(&#x27;new-audio-clip.mp3&#x27;)
  .complexFilter([
    &#x27;[1:0] adelay=2728000|2728000 [delayed]&#x27;,
    &#x27;[0:1][delayed] amix=inputs=2&#x27;,
  ])
  .outputOption(&#x27;-map 0:0&#x27;)
  .audioCodec(&#x27;aac&#x27;)
  .videoCodec(&#x27;copy&#x27;)
  .save(&#x27;output.mp4&#x27;)
</code></pre>
<h3 id="realization">Realization</h3>
<p>After writing all of this, I now realize that I actually need to silence the video's audio track
for the duration of the new audio clip. Not merge them.</p>
<p>Back to work.</p>
]]></content:encoded>
            <media:content url="https://cloudy.nyc/og-card.jpg" medium="image"/>
            <media:thumbnail url="https://cloudy.nyc/og-card.jpg"/>
        </item>
        <item>
            <title>A React Native library to run commands over SSH</title>
            <pubDate>Tue, 06 Dec 2016 00:00:00 +0000</pubDate>
            <author>Peter Valdez</author>
            <link>https://cloudy.nyc/blog/react-native-ssh/</link>
            <guid>https://cloudy.nyc/blog/react-native-ssh/</guid>
            <description>For my React Native app Hooks, I wrote some native Android code to enable the running of arbitrary commands over SSH.</description>
            <content:encoded xml:base="https://cloudy.nyc/blog/react-native-ssh/"><![CDATA[<p>For my React Native app <a href="https://github.com/azlyth/hooks">Hooks</a>, I wrote some native Android code to enable
the running of arbitrary commands over SSH.</p>
<p>I've now added the iOS side, and after some refactoring, the SSH code for both platforms is now its
own library: <a href="https://github.com/azlyth/react-native-ssh">react-native-ssh</a>.</p>
<p>There are no tests and it's very light on the error handling. But I figured I'd put it out there
for others to use and/or improve upon.</p>
<h2 id="native-dependency-pains">Native dependency pains</h2>
<p>Gradle (the Android build system) lets you specify dependencies from Maven (Java's package
repository). This meant that including the Android SSH library I needed was a single line. Nice and
easy.</p>
<p>Installing third-party dependencies on the iOS side of things is a pain, though. First, a
disclaimer: I'm by no means an iOS developer, and have only dealt with XCode and CocoaPods a
handful of times.</p>
<p>I first tried to make my library a pod to be installed via CocoaPods, which would allow it to have
its own dependencies. However, there are two problems with installing a React Native library as a
pod:</p>
<ul>
<li>it has to list React as one of its dependencies, which is deprecated</li>
<li>it's incompatible with <strong>react-native link</strong> which links your iOS library for you, and would result
in duplicate libraries in XCode</li>
</ul>
<p>After this didn't work, I decided to go with the manual installation methods listed by a few other
libraries (specifically <a href="/gone/react-native-lock/">react-native-lock</a>, thanks
auth0). So as it stands, after npm-installing <strong>react-native-ssh</strong>, you have to:</p>
<ul>
<li>run <strong>react-native link</strong> to include the library</li>
<li>install <strong>NMSSH</strong> to your own app via CocoaPods</li>
<li>update <strong>RNSSH</strong>'s Header Search Paths (react-native-ssh's name in XCode) to include your Pods
headers</li>
</ul>
<p>Perhaps I missed something, but if I haven't, this is definitely something to be addressed for
React Native.</p>
<h2 id="get-it-while-it-s-hot">Get it while it's hot</h2>
<p><strong>react-native-ssh</strong> is on <a href="https://github.com/azlyth/react-native-ssh">GitHub</a> and
<a href="https://www.npmjs.com/package/react-native-ssh">npm</a>.</p>
]]></content:encoded>
            <media:content url="https://cloudy.nyc/og-card.jpg" medium="image"/>
            <media:thumbnail url="https://cloudy.nyc/og-card.jpg"/>
        </item>
        <item>
            <title>A mobile app to run scripts on servers</title>
            <pubDate>Thu, 24 Nov 2016 00:00:00 +0000</pubDate>
            <author>Peter Valdez</author>
            <link>https://cloudy.nyc/blog/hooks/</link>
            <guid>https://cloudy.nyc/blog/hooks/</guid>
            <description>Over the years, whether I was working with a Raspberry Pi or running a server, there would often be one-off scripts that I would want to run. The usual pattern would be:</description>
            <content:encoded xml:base="https://cloudy.nyc/blog/hooks/"><![CDATA[<p>Over the years, whether I was working with a Raspberry Pi or running a server, there would often be
one-off scripts that I would want to run. The usual pattern would be:</p>
<ul>
<li>SSH into the server</li>
<li>Run a single command</li>
<li>Logout</li>
</ul>
<p>I always thought it would be nice to have a phone app that could easily run those scripts.</p>
<p>I'd try some mobile SSH client but those felt clunky; a full-blown terminal was overkill. I just
wanted to quickly run a single script.</p>
<p>Other times I'd write a quick Flask app to listen on obscure ports and URLs to trigger a script.
This also felt like overkill, though, because I really didn't want to have to expose some HTTP
server on some port just to run a script on occasion.</p>
<p>So I decided to bite the bullet and write the app that I wanted.</p>
<h2 id="hooks">Hooks</h2>
<p>The app is simple. It works as follows:</p>
<ul>
<li>Add a server's SSH credentials</li>
<li>You'll see a list of the executable files in <strong>~/.hooks-app/hooks</strong></li>
<li>Tap one of them and it will run</li>
</ul>
<p>I myself have started to use this for a few things, including making sure all the containers are
running properly on this server.</p>
<h2 id="what-i-learned">What I learned</h2>
<p>This was the first time I used React and it's libraries. I have to say that using it, particularly
together with Redux, felt elegant and safe. The fact that a global state defines the UI really
makes things simpler.</p>
<p>Working with React Native was also a treat. <strong>Hot Loading</strong> is a wonderful feature that will update
your app when you save a file, while maintaining your app's state. This makes UI work so much
easier.</p>
<p>I also got to write some native Android code. I used the JSch Java library to do the SSH
communication, so I had to write a bridge between that library and the app's JavaScript. Basically,
you write a new Java class that implements method (annotated with <strong>@ReactMethod</strong>) where certain
Java types map to the JS types.</p>
<p>Finally, I learned that ES6 is the bomb. Special shoutouts to the feature listed below.</p>
<p>Shorthand property names:</p>
<pre data-lang="js" class="language-js "><code class="language-js" data-lang="js">return { foo, bar, baz }
</code></pre>
<p>Object destructuring:</p>
<pre data-lang="js" class="language-js "><code class="language-js" data-lang="js">let { width: screenWidth } = Dimensions.get(&#x27;window&#x27;)
</code></pre>
<p>Arrow functions:</p>
<pre data-lang="js" class="language-js "><code class="language-js" data-lang="js">[1, 2, 3].map(x =&gt; x + 5)
</code></pre>
<p>Object spread operator:</p>
<pre data-lang="js" class="language-js "><code class="language-js" data-lang="js">{ ...currentState, ...newState }
</code></pre>
<p>Async / await:</p>
<pre data-lang="js" class="language-js "><code class="language-js" data-lang="js">let x = await somePromiseReturningMethod()
</code></pre>
<h2 id="get-the-app">Get the app</h2>
<p>I haven't written an iOS version of the native SSH bridge, so for now, the app only works on
Android.</p>
<ul>
<li><a href="/gone/hooks-play-store/">Google Play store</a></li>
<li><a href="https://github.com/azlyth/hooks">GitHub</a></li>
</ul>
]]></content:encoded>
            <media:content url="https://cloudy.nyc/og-card.jpg" medium="image"/>
            <media:thumbnail url="https://cloudy.nyc/og-card.jpg"/>
        </item>
        <item>
            <title>A simple image to backup volumes to S3</title>
            <pubDate>Fri, 21 Oct 2016 00:00:00 +0000</pubDate>
            <author>Peter Valdez</author>
            <link>https://cloudy.nyc/blog/docker-s3-backup/</link>
            <guid>https://cloudy.nyc/blog/docker-s3-backup/</guid>
            <description>One part of setting up my online presence naturally includes getting backups going.</description>
            <content:encoded xml:base="https://cloudy.nyc/blog/docker-s3-backup/"><![CDATA[<p>One part of setting up my online presence naturally includes getting backups going.</p>
<p>Since I'm trying to keep everything containerized, I did some research into what Amazon S3 backup
solutions are out there. I found a popular project called <a href="https://github.com/tutumcloud/dockup">dockup</a>
that basically does everything I wanted to have done.</p>
<p>However, upon looking at what I had to configure, I thought it could be simpler.</p>
<h2 id="how-dockup-works">How dockup works</h2>
<p>In order to get <strong>dockup</strong> working, you have to mount the volumes you want to backup to some
arbitrary place in the filesystem, and provide the following environment variables:</p>
<ul>
<li>AWS_ACCESS_KEY_ID</li>
<li>AWS_SECRET_ACCESS_KEY</li>
<li>AWS_DEFAULT_REGION</li>
<li>BACKUP_NAME</li>
<li>PATHS_TO_BACKUP</li>
<li>S3_BUCKET_NAME</li>
</ul>
<p>So, in English, given a bunch of directories (specified in PATHS_TO_BACKUP), <strong>dockup</strong> will
create a timestamped backup (using the name BACKUP_NAME) in the bucket S3_BUCKET_NAME.</p>
<h2 id="i-d-do-it-a-bit-differently">I'd do it a bit differently</h2>
<p>My ideal backup image would work different in a few ways.</p>
<p>First, I want a separate backup for each of my apps. At any given time, I'm not sure which services
I will be running. Maybe I'll stop the blog and keep the git repo running. Or maybe I'll add a new
service. I want to keep the data for each of those services separate.</p>
<p>Next, I shouldn't have to specify which directories I want to backup. I could just put my volumes
somewhere specific, like <strong>/backups</strong>.</p>
<p>Finally, the structure of the backup directory can contain the bucket and name. For example, any
data in the directory <strong>/backups/app-backups/blog-data</strong> will be bundled and named <strong>blog-data</strong>
and placed in the <strong>app-backups</strong> bucket.</p>
<h2 id="so-i-made-my-own">So I made my own</h2>
<p>With those ideas in mind, I made my own image called <strong>s3-backup</strong>. The following block is now live
in my Docker Compose file:</p>
<pre data-lang="yaml" class="language-yaml "><code class="language-yaml" data-lang="yaml">  backups:
    image: ptrvldz&#x2F;s3-backup:0.1
    container_name: backups
    restart: always
    volumes:
      - ghost:&#x2F;backups&#x2F;ptrvldz-backups&#x2F;ghost-blog
      - gogs:&#x2F;backups&#x2F;ptrvldz-backups&#x2F;gogs
    environment:
      - AWS_SECRET_ACCESS_KEY=$AWS_SECRET_ACCESS_KEY
      - AWS_ACCESS_KEY_ID=$AWS_ACCESS_KEY_ID
      - AWS_DEFAULT_REGION=$AWS_DEFAULT_REGION

</code></pre>
<p>All I have to do is provide my AWS credentials and put my volumes in the right places. With this
setup, the two following backups are placed in my <strong>ptrvldz-backups</strong> bucket:</p>
<ul>
<li>ghost-blog/ghost-blog-TIMESTAMP.tar.bz2</li>
<li>gogs/gogs-TIMESTAMP.tar.bz2</li>
</ul>
<p>You can find <strong>s3-backup</strong> on <a href="https://github.com/azlyth/docker-s3-backup">GitHub</a> and
<a href="https://hub.docker.com/r/ptrvldz/s3-backup/">Docker Hub</a>.</p>
]]></content:encoded>
            <media:content url="https://cloudy.nyc/og-card.jpg" medium="image"/>
            <media:thumbnail url="https://cloudy.nyc/og-card.jpg"/>
        </item>
        <item>
            <title>Anatomy of this site&#x27;s Docker Compose file</title>
            <pubDate>Mon, 17 Oct 2016 00:00:00 +0000</pubDate>
            <author>Peter Valdez</author>
            <link>https://cloudy.nyc/blog/using-docker-to-self-host/</link>
            <guid>https://cloudy.nyc/blog/using-docker-to-self-host/</guid>
            <description>As it stands, this server has two services running: a blog and git repository. They&apos;re both routed to by an nginx proxy, and my free SSL certificates are provided by the oh-so-wonderful Let&apos;s Encrypt.</description>
            <content:encoded xml:base="https://cloudy.nyc/blog/using-docker-to-self-host/"><![CDATA[<p>As it stands, this server has two services running: a blog and git repository. They're both routed
to by an nginx proxy, and my free SSL certificates are provided by the oh-so-wonderful Let's Encrypt.</p>
<p>The best part is that all of that is defined in and deployed by using a single Docker Compose file.</p>
<p>That means the only thing I had to do on my server was install
<a href="https://www.docker.com/what-docker">Docker</a> (which is made easy by
<a href="https://docs.docker.com/machine/overview/#/what-is-docker-machine">Docker Machine</a>).
The rest happens inside containers that play well with each other thanks to
<a href="https://docs.docker.com/compose/overview/#/overview-of-docker-compose">Docker Compose</a>.</p>
<p>I'm going to explain the file section by section, but first, here's all of it:</p>
<pre data-lang="yaml" class="language-yaml "><code class="language-yaml" data-lang="yaml">version: &#x27;2&#x27;

services:

  nginx-proxy:
    image: jwilder&#x2F;nginx-proxy:0.4.0
    container_name: nginx-proxy
    ports:
      - &quot;80:80&quot;
      - &quot;443:443&quot;
    volumes:
      - certs:&#x2F;etc&#x2F;nginx&#x2F;certs:ro
      - &#x2F;etc&#x2F;nginx&#x2F;conf.d
      - &#x2F;etc&#x2F;nginx&#x2F;vhost.d
      - &#x2F;usr&#x2F;share&#x2F;nginx&#x2F;html
      - &#x2F;var&#x2F;run&#x2F;docker.sock:&#x2F;tmp&#x2F;docker.sock:ro
    environment:
      - DEFAULT_HOST=ptrvldz.me

  letsencrypt-nginx-proxy:
    image: jrcs&#x2F;letsencrypt-nginx-proxy-companion
    container_name: letsencrypt-nginx-proxy
    volumes_from:
      - nginx-proxy
    volumes:
      - &#x2F;var&#x2F;run&#x2F;docker.sock:&#x2F;var&#x2F;run&#x2F;docker.sock:ro
      - certs:&#x2F;etc&#x2F;nginx&#x2F;certs:rw

  blog:
    image: ghost:0.11.2
    container_name: blog
    volumes:
      - ghost:&#x2F;var&#x2F;lib&#x2F;ghost
    environment:
      - VIRTUAL_HOST=ptrvldz.me
      - LETSENCRYPT_HOST=ptrvldz.me
      - LETSENCRYPT_EMAIL=webmaster@ptrvldz.me

  git:
    image: gogs&#x2F;gogs:0.9.97
    container_name: git
    volumes:
      - gogs:&#x2F;data
    environment:
      - VIRTUAL_HOST=git.ptrvldz.me
      - VIRTUAL_PORT=3000
      - LETSENCRYPT_HOST=git.ptrvldz.me
      - LETSENCRYPT_EMAIL=webmaster@ptrvldz.me

volumes:

  ghost:
    external: false
  gogs:
    external: false
  certs:
    external: false
</code></pre>
<h2 id="version">Version</h2>
<p>The first line defines which Docker Compose file format we're going to write. The recommended
format is the newer format, so we start the file with:</p>
<pre data-lang="yaml" class="language-yaml "><code class="language-yaml" data-lang="yaml">version: 2
</code></pre>
<h2 id="services">Services</h2>
<p>Next we get to the meat of the file, where we define our services. We begin our service definitions
with the line:</p>
<pre data-lang="yaml" class="language-yaml "><code class="language-yaml" data-lang="yaml"> services:
</code></pre>
<p>And away we go.</p>
<h2 id="service-nginx-proxy">Service: nginx proxy</h2>
<p>The nginx-proxy image is one of the most magical of them all.</p>
<p>Let's consider a usual desired setup: you want to host various web apps on different subdomains on
a single server. The way we typically solve this is by running a web server that forwards requests
to the desired applications.</p>
<p>There is usually a bunch of boilerplate involved in getting this going, but in essence, you're just
mapping a domain to some local port where your app is running.</p>
<p>The nginx-proxy image removes the boilerplate. Once running, it finds any containers that have the
<strong>VIRTUAL_HOST</strong> environment variable, and then forwards any requests bound for the domain defined by
the variable to that container.</p>
<p>In other words, if we start a WordPress container with the environment variable <strong>VIRTUAL_HOST</strong> set
to <strong>wordpress.somesite.com</strong>, then the nginx proxy will forward all requests for
<strong>wordpress.somesite.com</strong> to that WordPress container.</p>
<p>So let's look at the definition of the nginx-proxy:</p>
<pre data-lang="yaml" class="language-yaml "><code class="language-yaml" data-lang="yaml">  nginx-proxy:
    image: jwilder&#x2F;nginx-proxy:0.4.0
    container_name: nginx-proxy
    ports:
      - &quot;80:80&quot;
      - &quot;443:443&quot;
    volumes:
      - &#x2F;var&#x2F;run&#x2F;docker.sock:&#x2F;tmp&#x2F;docker.sock:ro
      - certs:&#x2F;etc&#x2F;nginx&#x2F;certs:ro
      - &#x2F;etc&#x2F;nginx&#x2F;conf.d
      - &#x2F;etc&#x2F;nginx&#x2F;vhost.d
      - &#x2F;usr&#x2F;share&#x2F;nginx&#x2F;html
    environment:
      - DEFAULT_HOST=ptrvldz.me
</code></pre>
<p>The first line is the name of our service. We then define which image we want to use and what we
want to call the container when it's running.</p>
<p>Because this is a web server that will be SSL-enabled, we specify that we want our actual
"hardware" server's port 80 and 443 to be forwarded to our nginx proxy.</p>
<p>We then define our various volumes. First, we give the container access to our host Docker socket
because that's how it will gather data about other containers. We then use a named volume
(identifiable by the slash-less string before the colon) for the SSL certificates because that's
where our letsencrypt container will place them. And then the remaining three volumes are defined
so that our letsencrypt container can write to the files in there.</p>
<p>Finally, we define the environment variable <strong>DEFAULT_HOST</strong> so the proxy knows which domain is
default, in case a request does not ask for a specific domain.</p>
<h2 id="service-ssl-certificate-generation">Service: SSL certificate generation</h2>
<p>If you've not yet heard of it, Let's Encrypt is a great project that allows you to get free SSL
certificates. With the easy-to-use clients that exist, it's all pretty much automatic.</p>
<p>And if you're using nginx-proxy, it's even easier. All we give our letsencrypt container is:</p>
<ul>
<li>access to our nginx-proxy volumes</li>
<li>the Docker socket so it can see which services need a certificate</li>
<li>a place to put the certificates</li>
</ul>
<p>Let's look at the service definition:</p>
<pre data-lang="yaml" class="language-yaml "><code class="language-yaml" data-lang="yaml">  letsencrypt-nginx-proxy:
    image: jrcs&#x2F;letsencrypt-nginx-proxy-companion
    container_name: letsencrypt-nginx-proxy
    volumes_from:
      - nginx-proxy
    volumes:
      - &#x2F;var&#x2F;run&#x2F;docker.sock:&#x2F;var&#x2F;run&#x2F;docker.sock:ro
      - certs:&#x2F;etc&#x2F;nginx&#x2F;certs:rw
</code></pre>
<p>The first three lines again define the name of the Docker Compose service, the image we're using,
and the name of the running container.</p>
<p>We then specify that we want it to have access to the volumes that we created for our
nginx-proxy.</p>
<p>And finally, we give it access to the Docker socket and tell it to use a named volume for the
certs. Note that the named volume here (<strong>certs</strong>) is the same as the named volume being accessed by
the nginx-proxy.</p>
<h2 id="service-blog">Service: Blog</h2>
<p>We finally get to an actual application, a blog.</p>
<p>I knew I didn't want to use something heavy like Wordpress. And I love static-site generators, but
I have to spend some time thinking about a setup.</p>
<p>So I went with a compromise: Ghost. I can self-host it, and posts are written in Markdown, so I can
port to some static-site generation setup with ease later.</p>
<p>So now that all our preparation was done in the previous service definitions, let's see how we can
define our Ghost blog:</p>
<pre data-lang="yaml" class="language-yaml "><code class="language-yaml" data-lang="yaml">  blog:
    image: ghost:0.11.2
    container_name: blog
    volumes:
      - ghost:&#x2F;var&#x2F;lib&#x2F;ghost
    environment:
      - VIRTUAL_HOST=ptrvldz.me
      - LETSENCRYPT_HOST=ptrvldz.me
      - LETSENCRYPT_EMAIL=webmaster@ptrvldz.me
</code></pre>
<p>Again, the first three lines are the Docker Compose service name, the image that we'll be using,
and container name.</p>
<p>We then define a named volume for our data because we want our blog data to exist even if we
recreate this container.</p>
<p>And now the magic.</p>
<ul>
<li>By setting <strong>VIRTUAL_HOST</strong> to <strong>ptrvldz.me</strong>, our nginx-proxy will know to forward requests for
<strong>ptrvldz.me</strong> to this container.</li>
<li>By setting <strong>LETSENCRYPT_HOST</strong> and <strong>LETSENCRYPT_EMAIL</strong>, our letsencrypt container will use that
data to create SSL certificates for this service.</li>
</ul>
<h2 id="service-git-repository">Service: Git repository</h2>
<p>And so our final service, the Git repository. I went with <a href="https://gogs.io">Gogs</a>. I originally
liked Gogs because of it's portability, thanks to it being a single binary. Considering I'm running
it inside a container, that doesn't matter as much, but oh well.</p>
<p>Let's take a look at the service definition:</p>
<pre data-lang="yaml" class="language-yaml "><code class="language-yaml" data-lang="yaml">  git:
    image: gogs&#x2F;gogs:0.9.97
    container_name: git
    volumes:
      - gogs:&#x2F;data
    environment:
      - VIRTUAL_HOST=git.ptrvldz.me
      - VIRTUAL_PORT=3000
      - LETSENCRYPT_HOST=git.ptrvldz.me
      - LETSENCRYPT_EMAIL=webmaster@ptrvldz.me
</code></pre>
<p>It's all the same as the blog, but there is one difference: <strong>VIRTUAL_PORT</strong>.</p>
<p>If a container only exposes a single port, then our nginx-proxy is smart enough to know it should
forward requests to that port. However, if several ports are exposed by a container, you can
specify which one is correct by setting <strong>VIRTUAL_PORT</strong> to the correct one.</p>
<h2 id="volumes">Volumes</h2>
<p>The last section of the file is the volumes block. If you use named volumes, you must define them
in this section, and so we do:</p>
<pre data-lang="yaml" class="language-yaml "><code class="language-yaml" data-lang="yaml">volumes:

  ghost:
    external: false
  gogs:
    external: false
  certs:
    external: false
</code></pre>
<p>Setting <strong>external</strong> to <strong>false</strong> for our volumes will tell Docker Compose that it should create them
if they're not there.</p>
<h2 id="and-we-re-done">And we're done</h2>
<p>So to reiterate, we have nginx forwarding requests to two applications, each with valid SSL
certificates. With each distinct service in its own container. All this accomplished with 57 lines
of configuration in a single file.</p>
<p>And what if we wanted to add one more service, like that WordPress instance we mentioned earlier?
Probably about 10 more lines and we'd have it routed to by nginx and secured with its own
certificate.</p>
<p>I thank the powers that be for containers.</p>
]]></content:encoded>
            <media:content url="https://cloudy.nyc/og-card.jpg" medium="image"/>
            <media:thumbnail url="https://cloudy.nyc/og-card.jpg"/>
        </item>
        <item>
            <title>Back online</title>
            <pubDate>Sun, 16 Oct 2016 00:00:00 +0000</pubDate>
            <author>Peter Valdez</author>
            <link>https://cloudy.nyc/blog/back-online/</link>
            <guid>https://cloudy.nyc/blog/back-online/</guid>
            <description>It&apos;s been a while since I&apos;ve had any form of personal site.</description>
            <content:encoded xml:base="https://cloudy.nyc/blog/back-online/"><![CDATA[<p>It's been a while since I've had any form of personal site.</p>
<p>Every time I wanted to start one, I would overthink it.</p>
<p>I'd think about the various apps I wanted to host, like a blog, a git repository, a file server, maybe even my own mail. And it'd be all nicely organized and deployable.</p>
<p>But in the end, all I'd do was think and not do.</p>
<p>So today, simple beginnings:</p>
<ul>
<li>A blog at <a href="/gone/ptrvldz-me/">ptrvldz.me</a></li>
<li>A git repo at <a href="/gone/git-ptrvldz-me/">git.ptrvldz.me</a></li>
</ul>
]]></content:encoded>
            <media:content url="https://cloudy.nyc/og-card.jpg" medium="image"/>
            <media:thumbnail url="https://cloudy.nyc/og-card.jpg"/>
        </item>
    </channel>
</rss>
