---
name: stellaris-implement
description: "Translate Stellaris mod designs into defensive Paradox Script."
---

## OBJECTIVE

**THE CONSTRUCTOR**: Translate Design (ADR) into high-quality, defensive Paradox Script (`.txt`).
**Philosophy**: "Trust No One." The Engine is fragile. Assume every scope is null, every target is missing, and every bracket is unbalanced until proven otherwise.

## PROTOCOL

### Phase 1: File Genesis

**Constraint**: All files reside in `src/`.
**Action**: Create files based on the Manifest.

1. **Standard Header**:

    ```pdx
    # ==============================================================================
    # MOD: [Mod Name]
    # FILE: [Relative Path from src/]
    # PURPOSE: [Brief Description]
    # ==============================================================================
    ```

2. **Namespace Declaration**:
    - **Rule**: If the file contains `events`, the *first functional line* must be `namespace = [NS]`.
    - **Constraint**: The namespace must match the project prefix defined in `init`.

### Phase 2: Logic Implementation (The Iron Rules)

**Input**: Pseudocode from `design`.
**Action**: Write PDX Script obeying these laws:

1. **Defensive Scoping (The Shield)**:
    - **Rule**: Never run an effect on a scope without checking existence.
    - **Pattern**:

        ```pdx
        if = {
            limit = { exists = owner type = country }
            owner = { ... }
        }
        ```

2. **Scope Hygiene (The Tunnel)**:
    - **Rule**: When using iterators (`every_system`, `random_planet`), ALWAYS use `limit = { }` inside the effect block to prevent "Scope Leaks" (affecting the wrong target).
3. **Variable Hygiene**:
    - **Rule**: No magic numbers (>2 uses). Use `@scripted_variables`.
    - **Pattern**: `cost = @[NS]_tier1_cost` (Defined at top of file or in `common/scripted_variables`).
4. **Localisation Markers**:
    - **Rule**: Do not write raw English text in `title` or `desc`.
    - **Pattern**: `title = "[NS]_event_1_title" # TODO: LOC`

### Phase 3: Static Analysis (Self-Correction)

**Before Output**, verify:

1. **Bracket Balance**: Count `{` vs `}`. Imbalance causes silent crashes.
2. **Smart Quotes**: Ensure NO “curly quotes” exist. Only standard ASCII `""`.
3. **Dependency Check**: If using `add_modifier = [NS]_mod`, ensure `common/modifiers/[NS]_modifiers.txt` is planned.

## OUTPUT TEMPLATE

```pdx
# ==============================================================================
# MOD: Stellaris Expanded
# FILE: events/[NS]_anomaly_events.txt
# ==============================================================================

namespace = [NS]

# [NS].1 - The Discovery
country_event = {
    id = [NS].1
    title = "[NS]_event_1_title" # TODO: LOC
    desc = "[NS]_event_1_desc"   # TODO: LOC
    picture = GFX_evt_mining_station

    is_triggered_only = yes

    trigger = {
        exists = owner
        owner = { is_ai = no }
    }

    immediate = {
        owner = {
            log = "DEBUG: [NS].1 fired for [This.GetName]"
            add_resource = {
                minerals = @[NS]_reward_tier_1
            }
        }
    }

    option = {
        name = "[NS]_event_1_opt_a" # TODO: LOC
        custom_tooltip = "[NS]_event_1_opt_a_tt"
    }
}

```

## EXECUTION RULES

1. **Atomic Writes**: Generate one file content block at a time.
2. **No Placeholders**: Do not write `# logic goes here`. Write the logic.
3. **Indentation**: Use **Tabs** (Standard Paradox) or **4 Spaces**. Be consistent.
