// Create the character
IfSpawned
  IfStateIs0
    // It's an imported spell in hand
    KeepAction
  Else
    // Zombi speed based on state
    MakeNameUnknown
    SetTargetToSelf
    NotAnItem
    tmpx = selfstate
    SpawnPoof
    IfStateIs1
      tmpargument = 0 - 1500
      GiveLifeToTarget
      tmpargument = 25
    IfStateIs2
      tmpargument = 0 - 700
      GiveLifeToTarget
      tmpargument = 50
    IfStateIs3
      tmpargument = 100
    SetSpeedPercent
    tmpargument = 50
    SetTime
IfChanged
  // It's a spell in hand
  tmpargument = 0
  SetState
  KeepAction


// Monster AI
IfStateIs0
  GetState // NOP
Else
  // Character screams
  IfAttacked
    tmpargument = 3
    tmpdistance = rand & 2047 + 10000
    PlaySound


  // Character death
  IfKilled
    // Make the character body
    tmpargument = 45
    SetBumpHeight
    // Tell everyone it died
    tmpargument = 4
    tmpdistance = rand & 2047 + 10000
    PlaySound
    tmpargument = 0
    IfTargetIsOnSameTeam
      tmpargument = 1
      IfTargetIsSelf
        tmpargument = 2
    SendMessageNear


  // Do different Zombi AI's
  IfStateIs1
    // Stupid AI
    IfTimeOut
      // Movement
      tmpx = rand & 511 + ownerx - 256
      tmpy = rand & 511 + ownery - 256
      ClearWaypoints
      AddWaypoint
      // Reset the timer
      tmpargument = 200
      SetTime
      tmpargument = rand & 1 + 1
      tmpdistance = rand & 2047 + 10000
      PlaySound
    IfBumped
      // Attack anyone
      tmpargument = rand & 2 + ACTIONBA
      DoAction  // Couldn't use latches...
  IfStateIs2
    // Middle level AI
    IfTimeOut
      // Movement
      SetTargetToWideEnemy
        // Actively look for enemies
        tmpx = targetdistance
        tmpy = 600
        IfXIsLessThanY
          // Attack the enemy?
          tmpy = 100
          IfXIsLessThanY
            // Swing left or right
            tmpargument = rand & 2 + ACTIONBA
            DoAction  // Couldn't use latches...
            tmpargument = rand & 1 + 1
            tmpdistance = rand & 2047 + 10000
            PlaySound
          // Chase the enemy
          tmpx = targetx
          tmpy = targety
        Else
          tmpx = rand & 511 + ownerx - 256
          tmpy = rand & 511 + ownery - 256
      Else
        tmpx = rand & 511 + ownerx - 256
        tmpy = rand & 511 + ownery - 256
      ClearWaypoints
      AddWaypoint
      // Reset the timer
      tmpargument = 100
      SetTime
  IfStateIs3
    // Bad boy AI
    IfTimeOut
      // Mana drips
      tmpx = selfx
      tmpy = selfy
      tmpdistance = selfz
      tmpargument = 3
      SpawnExactParticle
      // Movement
      tmpx = ownerx
      tmpy = ownery
      SetTargetToNearestFriend
        tmpturn = targetturnto + 32768
        tmpdistance = 100
        Compass
      // Save the values for X and Y...
      tmpargument = 0
      SetXY
      tmpx = ownerdistance
      tmpy = 500
      IfXIsLessThanY
        SetTargetToNearestEnemy
          // Attack the enemy?
          SetTurnModeToWatchTarget
          tmpx = targetdistance
          tmpy = 140
          IfXIsLessThanY
            IfFacingTarget
              // Check for good weapons
              IfHoldingMeleeWeapon
                tmpargument = [MACE]
                IfHoldingItemID
                  // Bash already
                  tmpargument = tmpargument - 1 < 1 + ACTIONBB
                Else
                  tmpargument = [HAMM]
                  IfHoldingItemID
                    // Bash already
                    tmpargument = tmpargument - 1 < 1 + ACTIONBB
                  Else
                    tmpargument = [AXEE]
                    IfHoldingItemID
                      // Bash already
                      tmpargument = tmpargument - 1 < 1 + ACTIONBB
                    Else
                      tmpargument = [CLAW]
                      IfHoldingItemID
                        // Bash already
                        tmpargument = tmpargument - 1 < 1 + ACTIONBB
                      Else
                        // Hmmm...  Not holding a good weapon, so better drop it
                        DropWeapons
                        tmpargument = rand & 2 + ACTIONBB
              Else
                // Swing left or right
                tmpargument = rand & 2 + ACTIONBB
              DoAction  // Couldn't use latches...
              tmpargument = rand & 1 + 1
              tmpdistance = rand & 2047 + 10000
              PlaySound
          // Chase the enemy
          tmpargument = 0
          GetXY  // Restore the saved values...
          tmpturn = targetturnto
          tmpdistance = 600
          Compass
        Else
          SetTurnModeToVelocity
          // Try finding an item...
          IfHoldingMeleeWeapon
            // Already have one, so run after owner
            SetTargetToOwner
            tmpx = targetdistance
            tmpy = 300
            IfXIsMoreThanY
              tmpx = targetx
              tmpy = targety
              tmpturn = targetturnto
              tmpdistance = 600
              Compass
            Else
              tmpargument = 0
              GetXY
          Else
            // Find a good weapon...  Hammers, Maces, Axes, and Claws
            tmpargument = [HAMM]
            tmpdistance = BLAHITEMS
            tmpx = 0
            SetTargetToNearestBlahID
              tmpx = 1
            Else
              tmpargument = [MACE]
              SetTargetToNearestBlahID
                tmpx = 1
              Else
                tmpargument = [AXEE]
                SetTargetToNearestBlahID
                  tmpx = 1
                Else
                  tmpargument = [CLAW]
                  SetTargetToNearestBlahID
                    tmpx = 1
            tmpy = 1
            IfXIsEqualToY
              tmpx = targetdistance
              tmpy = 50
              IfXIsLessThanY
                tmpargument = rand & 1 + ACTIONME
                DoAction
              tmpx = targetx
              tmpy = targety
              tmpturn = targetturnto
              tmpdistance = 100
              Compass
            Else
              // Couldn't find any items, so run after owner
              tmpargument = 0
              GetXY
      Else
        // Catch up to owner
        tmpargument = 0
        GetXY
      ClearWaypoints
      AddWaypoint
      // Reset the timer
      tmpargument = 10
      SetTime


// Spell AI
IfStateIs0
  // Remove the charge
  IfTakenOut
    tmpargument = 0
    SetContent
    SetTargetToWhoeverIsHolding
      IfTargetIsAPlayer
        tmpargument = 5
        IfNameIsKnown
          tmpargument = 6
        SendMessageNear
  // Allow it to be used
  IfUsed
    SetTargetToWhoeverIsHolding
    tmpargument = [WMAG]
    IfTargetHasSkillID
      GetContent
      tmpx = tmpargument
      tmpy = 768
      IfXIsLessThanY
        tmpy = targetmanaflow
        IfXIsLessThanY
          tmpx = 4
          tmpargument = tmpx
          CostTargetMana
            GetContent
            tmpargument = tmpargument + tmpx
            SetContent
            tmpx = targetx
            tmpy = targety
            tmpdistance = targetz
            tmpargument = 4
            SpawnExactParticle
      // Put particles around the target
      tmpargument = [HUMA]
      tmpdistance = 7  // Dead Friends and Enemies...
      SetTargetToNearestBlahID
        tmpx = targetx
        tmpy = targety
        tmpdistance = targetz
        tmpargument = 4
        SpawnExactParticle
  Else
    GetContent
    tmpx = tmpargument
    tmpy = 255
    IfXIsMoreThanY
      // Make the holder cast it
      SetTargetToWhoeverIsHolding
        tmpargument = ACTIONMC // MC because ZA/ZC spawns an attack... ZA03AL
        TargetDoAction
      // Find a dead humanoid
      tmpargument = [HUMA]
      tmpdistance = 7  // Dead friends or enemies...
      SetTargetToNearestBlahID
        // Poof that target ( players can't be )
        MakeNameKnown
        MakeUsageKnown
        tmpx = targetx
        tmpy = targety
        tmpturn = targetturn
        tmpdistance = targetz
        PoofTarget
          // Replace the target with a Zombi
          SetTargetToWhoeverIsHolding
          SetOwnerToTarget
          SpawnCharacterXYZ
            EnchantChild


            // Set the drain
            GetContent
            tmpargument = 0 - tmpargument > 3
            tmpdistance = 0
            tmpx = 0
            tmpy = 0
            SetEnchantBoostValues


            // Set the Zombi type
            GetContent
            tmpargument = tmpargument > 8 // State of new Zombi, 0 to 3
            SetChildState
            tmpargument = ACTIONMD
            ChildDoActionOverride
          tmpargument = 0
          tmpdistance = 22050
          PlaySound
          tmpargument = 20
          SetReloadTime
        Else
          // Couldn't poof it
          tmpargument = 3
          SendMessageNear
      Else
        // Couldn't find a body
        tmpargument = 4
        SendMessageNear
    Else
      tmpy = 0
      IfXIsMoreThanY
        // Didn't pump it enough
        tmpargument = 4
        SendMessageNear
    // Reset the charge counter
    tmpargument = 0
    SetContent
  // Return to spellbook, Do last!
  IfDropped
    tmpargument = 0
    SetContent
    BecomeSpellbook
    DisaffirmCharacter
    tmpargument = ACTIONJB
    DoAction
    KeepAction
End
