# Spell Processing System

Comprehensive spell casting engine managing all aspects of spell processing including timers, targeting, resource management, and heroic opportunities.

## Components

**SpellProcess** - Core engine managing active spells, cast/recast timers, interrupt queues, spell queues, and heroic opportunities
**SpellTargeting** - Target selection for all spell types (self, single, group, AOE, PBAE) with validation
**SpellResourceChecker** - Resource validation/consumption for power, health, concentration, savagery, dissonance
**SpellManager** - High-level coordinator integrating all systems with comprehensive casting API

## Key Data Structures

```go
// Active spell instance
type LuaSpell struct {
    Spell         *Spell    // Spell definition
    CasterID      int32     // Casting entity
    Targets       []int32   // Target list
    Timer         SpellTimer // Duration/tick timing
    NumCalls      int32     // Tick count
    Interrupted   bool      // Interrupt state
}

// Cast timing
type CastTimer struct {
    CasterID    int32         // Casting entity
    SpellID     int32         // Spell being cast
    StartTime   time.Time     // Cast start
    Duration    time.Duration // Cast time
}

// Cooldown timing
type RecastTimer struct {
    CasterID    int32         // Entity with cooldown
    SpellID     int32         // Spell on cooldown
    Duration    time.Duration // Cooldown time
    LinkedTimer int32         // Shared cooldown group
}
```

## Usage

```go
// Main spell processing loop (50ms intervals)
spellManager.ProcessSpells()

// Cast spell with full validation
err := spellManager.CastSpell(casterID, targetID, spellID)

// Interrupt casting
spellManager.InterruptSpell(entityID, spellID, errorCode, canceled, fromMovement)

// Queue management
spellManager.AddSpellToQueue(spellID, casterID, targetID, priority)
spellManager.RemoveSpellFromQueue(spellID, casterID)

// Status checks
ready := spellManager.IsSpellReady(spellID, casterID)
recastTime := spellManager.GetSpellRecastTime(spellID, casterID)
canCast, reason := spellManager.CanCastSpell(casterID, targetID, spellID)

// Resource checking
resourceChecker := spellManager.GetResourceChecker()
powerResult := resourceChecker.CheckPower(luaSpell, customPowerReq)
allResults := resourceChecker.CheckAllResources(luaSpell, 0, 0)
success := resourceChecker.ConsumeAllResources(luaSpell, 0, 0)

// Targeting
targeting := spellManager.GetTargeting()
targetResult := targeting.GetSpellTargets(luaSpell, options)
```

## Effect Types (80+)

**Stat Modifications**: Health, power, stats (STR/AGI/STA/INT/WIS), resistances, attack, mitigation
**Spell Modifications**: Cast time, power req, range, duration, resistibility, crit chance
**Actions**: Damage, healing, DOT/HOT, resurrect, summon, mount, invisibility
**Control**: Stun, root, mez, fear, charm, blind, kill
**Special**: Change race/size/title, faction, exp, tradeskill bonuses

## Target Types

**TargetTypeSelf** (0) - Self-only spells
**TargetTypeSingle** (1) - Single target with validation
**TargetTypeGroup** (2) - Group members
**TargetTypeGroupAE** (3) - Group area effect
**TargetTypeAE** (4) - True area effect
**TargetTypePBAE** (5) - Point blank area effect

## Interrupt System

**Causes**: Movement, damage, stun, mesmerize, fear, manual cancellation, out of range
**Processing**: Queued interrupts processed every cycle with proper cleanup
**Error Codes**: Match client expectations for proper UI feedback

## Performance

- **50ms Processing**: Matches client update expectations
- **Efficient Indexing**: Fast lookups by caster, spell, target
- **Thread Safety**: Concurrent access with proper locking
- **Memory Management**: Cleanup of expired timers and effects
- **Batch Operations**: Multiple resource checks/targeting in single calls

## Integration Points

**Entity System**: Caster/target info, position data, combat state
**Zone System**: Position validation, line-of-sight, spawn management
**Group System**: Group member targeting and coordination
**Database**: Persistent spell data, character spell books
**Packet System**: Client communication for spell states
**LUA System**: Custom spell scripting (future)
