# meta
```json
{
  "id": "flaregeddon",
  "name": "Flaregeddon",
  "tickRate": 10,
  "permissions": ["overlay.entities"]
}
```

# params
```json
{
  "durationSeconds": { "type": "number", "label": "Duration seconds", "default": 30 },
  "maxSpawned": { "type": "number", "label": "Max spawned", "default": 24 },
  "maxCurrent": { "type": "number", "label": "Max current", "default": 24 },
  "stopOnMaxSpawned": { "type": "boolean", "label": "Stop on max spawned", "default": false },
  "stopOnMaxCurrent": { "type": "boolean", "label": "Stop on max current", "default": false },
  "spawnEveryMs": { "type": "number", "label": "Spawn every ms", "default": 400 },
  "petId": { "type": "pet", "label": "Pet", "default": "flare" },
  "size": { "type": "number", "label": "Size", "default": 256 },
  "speed": { "type": "number", "label": "Speed", "default": 180 },
  "cleanup": { "type": "boolean", "label": "Cleanup", "default": true }
}
```

# select
```json
{}
```

# state
```json
{
  "$script": {
    "elapsedSeconds": 0,
    "spawnedCount": 0,
    "currentCount": 0,
    "spawnCooldownMs": 0
  }
}
```

# start
```json
[
  { "log": "Flaregeddon started" },
  {
    "removeEntities": {
      "idStartsWith": "flaregeddon-flare"
    }
  },
  {
    "runScript": {
      "id": "walk-edge-to-edge",
      "instanceId": "flaregeddon-walk",
      "parameters": {
        "idStartsWith": "flaregeddon-flare",
        "petId": "param.petId",
        "speed": "param.speed"
      }
    }
  }
]
```

# tick
```json
[
  {
    "do": [
      { "set": "script.elapsedSeconds", "value": "script.elapsedSeconds + dt" },
      { "set": "script.spawnCooldownMs", "value": "script.spawnCooldownMs - (dt * 1000)" },
      {
        "countEntities": {
          "idStartsWith": "flaregeddon-flare",
          "visible": true,
          "save": "script.currentCount"
        }
      },
      {
        "when": "script.spawnCooldownMs <= 0 && (param.maxSpawned <= 0 || script.spawnedCount < param.maxSpawned) && (param.maxCurrent <= 0 || script.currentCount < param.maxCurrent)",
        "do": [
          {
            "runScript": {
              "id": "spawn-pet",
              "parameters": {
                "petId": "param.petId",
                "idPrefix": "flaregeddon-flare",
                "name": "Flaregeddon flare {script.spawnedCount}",
                "state": "idle",
                "transitionStyle": "energy-bloom",
                "x": "random(workarea.left, workarea.right - param.size)",
                "y": "random(workarea.top, workarea.bottom - param.size)",
                "size": "param.size"
              }
            }
          },
          { "set": "script.spawnedCount", "value": "script.spawnedCount + 1" },
          { "set": "script.spawnCooldownMs", "value": "param.spawnEveryMs" },
          { "log": "Flaregeddon counters: elapsed={script.elapsedSeconds}, spawned={script.spawnedCount}, current={script.currentCount}" }
        ]
      },
      { "stopWhen": "(param.durationSeconds > 0 && script.elapsedSeconds >= param.durationSeconds) || (param.stopOnMaxSpawned && param.maxSpawned > 0 && script.spawnedCount >= param.maxSpawned) || (param.stopOnMaxCurrent && param.maxCurrent > 0 && script.currentCount >= param.maxCurrent)" }
    ]
  }
]
```

# stop
```json
[
  {
    "countEntities": {
      "idStartsWith": "flaregeddon-flare",
      "visible": true,
      "save": "script.currentCount"
    }
  },
  { "log": "Flaregeddon stopping: elapsed={script.elapsedSeconds}, spawned={script.spawnedCount}, current={script.currentCount}, cleanup={param.cleanup}" },
  { "stopScript": { "instanceId": "flaregeddon-walk" } },
  {
    "when": "param.cleanup",
    "do": [
      {
        "removeEntities": {
          "idStartsWith": "flaregeddon-flare"
        }
      },
      { "log": "Flaregeddon cleanup complete" }
    ]
  }
]
```
