//This is a special Zombi script, used in the Zombi starter

//Boss has been killed
IfOrdered
  tmpx = selforder
  tmpy = [UNDO]
  IfXIsEqualToY
    UndoEnchant

//Show location on map
tmpx = selfx
tmpy = selfy
tmpargument = BLUE
ShowBlipXY

//Randomize join team
IfSpawned
  SetTargetToSelf
  SetOwnerToTarget
  EnchantTarget
  tmpargument = 50
  SetTime
  
  tmpargument = rand % 4
  ChangeArmor

//  tmpx = rand % 100
//  tmpy = 34		//66% chance to join Necromancer
//  IfXIsLessThanY
//    tmpargument = 25	//Team Z
//  Else
//    tmpargument = 4	//Team E
//  JoinTeam

//--------------------------------------------------------------------------------------------
//This makes zombi mount mountable creatures
IfBumped
  SetTargetToWhoeverBumped
  tmpargument = [CHES]
  IfTargetHasID
    tmpargument = LATCHJUMP
    PressLatchButton			//Try to mount
  IfTargetIsAMount			//Bumper is a mount?
    tmpargument = LATCHJUMP
    PressLatchButton			//Try to mount
  Else
    SetTargetToOldTarget		//Nope, continue

//--------------------------------------------------------------------------------------------
// Character screams
IfAttacked
  tmpargument = 3
  PlaySound

//--------------------------------------------------------------------------------------------
// Character death
IfKilled
  // Make the character body
  tmpargument = 45
  SetBumpHeight

  // Tell everyone it died
  tmpargument = 4
  PlaySound
  tmpargument = 0
  IfTargetIsOnSameTeam
    tmpargument = 1
    IfTargetIsSelf
      tmpargument = 2
  SendMessageNear
  DropMoney		//Drop the loot

//--------------------------------------------------------------------------------------------
// Do the Zombi AI
//--------------------------------------------------------------------------------------------
IfTimeOut

  //STATE 0 - FOLLOW LEADER
  IfStateIs0
    SetTargetToLeader
    
    Run
    tmpx = rand & 2047 + targetx - 1024
    tmpy = rand & 2047 + targety - 1024
    tmpdistance = 1200
    tmpturn = targetturnto //rand & 31000 + targetturnto - 16000
    Compass
    ClearWaypoints
    AddWaypoint

    //Watch out for enemies
    SetTargetToWideEnemy
      tmpargument = 1
      SetState

    //Search for weapons if not holding one
    IfHoldingMeleeWeapon
      DoNothing					//Already found one
    Else
      // Find a good weapon...  Hammers, Maces, Axes, and Claws
      tmpargument = [HAMM]
      tmpdistance = BLAHITEMS
      tmpx = 0
      SetTargetToWideBlahID
        tmpx = 1
      Else
        tmpargument = [MACE]
        SetTargetToWideBlahID
          tmpx = 1
        Else
          tmpargument = [AXEE]
          SetTargetToWideBlahID
            tmpx = 1
          Else
            tmpargument = [CLAW]
            SetTargetToWideBlahID
              tmpx = 1
            Else
              tmpargument = [SWOR]
              SetTargetToWideBlahID
                tmpx = 1
      tmpy = 1
      IfXIsEqualToY			//If tmpx is 1, then a weapon was found
        tmpx = targetdistance
        tmpy = 80
        IfXIsLessThanY			//Close enough to grab it?
          tmpargument = rand & 1 + ACTIONME
          DoAction
        tmpx = targetx
        tmpy = targety
        tmpturn = targetturnto
        tmpdistance = 100
        Compass
        Run
        ClearWaypoints
        AddWaypoint
        
    // Reset the timer
    tmpargument = rand & 31 + 40
    SetTime

  //STATE 1 - COMBAT
  IfStateIs1
    Run
    
    //Leader is too far away
    tmpx = leaderdistance
    tmpy = 800
    IfXIsMoreThanY
      tmpargument = 0
      SetState
      
    SetTargetToWideEnemy			// Actively look for enemies
      tmpx = targetdistance
      tmpy = 600
      IfXIsLessThanY
        
        // Attack the enemy?
        tmpy = 200
        IfXIsLessThanY
          IfHoldingMeleeWeapon
            PressLatchButton
          Else
            // Swing left or right
            tmpargument = rand & 1 + LATCHLEFT
            PressLatchButton
          tmpargument = rand & 1 + 1
          PlaySound

        // Chase the enemy
        Run
        tmpx = targetx
        tmpy = targety
        tmpdistance = 1500
        tmpturn = targetturnto
        Compass
        ClearWaypoints
        AddWaypoint
      Else					//Too far away to engage. Follow leader
        tmpargument = 0
        SetState
    Else
      tmpargument = 0				//No enemies found
      SetState

    // Reset the timer
    tmpargument = rand & 15 + 27
    SetTime

//Move out of way of friendlies and dont get stuck in pillar
IfBumped
  SetTargetToWhoeverBumped
  tmpargument = [PILL]
  IfTargetHasID
    tmpx = 6000           //Ugly fix! Dont' get stuck in that pillar
    tmpy = 5600
    ClearWaypoints
    AddWaypoint
  Else
    IfTargetIsOnHatedTeam
      DoNothing
    Else
      tmpx = rand & 511 + targetx - 512
      tmpy = rand & 511 + targety - 512
      tmpturn = targetturnto
      tmpdistance = 0 - 600
      ClearWaypoints
      AddWaypoint
      SetTargetToOldTarget
End
