//States
//0 - Normal, saying hello
//1 - Combat
//2 - Return to normal
//3 - Waiting for brother
//4 - Return to waiting for brother

//Randomize starting position
IfSpawned
  tmpargument = rand % 5
  SetState
  IfStateIs0            //?
    tmpx = 102 < 7
    tmpy = 80 < 7
  IfStateIs1            //On house "roof"
    tmpx = 52 < 7
    tmpy = 53 < 7
  IfStateIs2            //?
    tmpx = 25 < 7
    tmpy = 66 < 7
  IfStateIs3            //Backyard
    tmpx = 7 < 7
    tmpy = 48 < 7
  IfStateIs4            //?
    tmpx = 110 < 7
    tmpy = 58 < 7
  Teleport
    DoNothing

  //This is bad! The coordinate above did not work!
  Else
    tmpx = 88 < 7
    tmpy = 13 < 7
    Teleport            //Use this one instead

  tmpargument = 0
  SetXY
  SetState

//Someone is looking for her?
IfOrdered
  tmpx = selforder
  tmpy = [BSPK]
  IfXIsEqualToY
    tmpargument = 3
    SetState

//Handle talking
IfBumped
  SetTargetToWhoeverBumped
  IfTargetIsAPlayer
    IfTargetIsOnSameTeam
      IfStateIs0
        tmpargument = [FORT]
        IfTargetHasQuest
          DoNothing
        tmpturn = 2
        IfDistanceIsMoreThanTurn
          tmpargument = 2
          IfContentIs
            tmpargument = 12        //Lets go again sometime...
          Else
            tmpargument = 11        //hello its you again
        Else
          tmpargument = 6           //Just say hello to strangers...
        PlayFullSound
        SendMessage
        tmpargument = 2
        SetState
        tmpargument = 100
        SetTime
      IfStateIs3                    //Standing at spawn point
        GetContent
        tmpx = tmpargument
        tmpy = 0
        IfXIsEqualToY
          tmpargument = 7
        tmpy = 1
        IfXIsEqualToY
          tmpargument = 8
        tmpy = 2
        IfXIsEqualToY

          //Add quest to the log and unlock the next module
          tmpargument = [FORT]
          tmpdistance = 2           //The quest level
          AddQuestAllPlayers        //Add it
          IssueOrder
          tmpargument = 9       //Unlocked!
        tmpy = 2
        IfXIsMoreThanY          //All done talking, loop this
          tmpargument = 10
        SendMessage
        PlayFullSound

        tmpargument = 4
        SetState                    //Wait before talking
        tmpargument = 150
        SetTime
        GetContent
        tmpargument = tmpargument + 1
        SetContent

//become bad
IfCalledForHelp
  JoinEvilTeam
  tmpargument = 1               //Combat
  SetState

//Ouch
IfAttacked
  SetTargetToWhoeverAttacked
  IfTargetIsOnSameTeam
    tmpargument = 3
  Else
    tmpargument = 4
  PlaySound

//Yah!
IfUsed
  tmpargument = 5
  PlaySound

//Death stuff
IfKilled
  tmpargument = selfmoney
  DropMoney

  //Sound
  tmpargument = 1
  PlaySound

  //Message
  IfTargetIsSelf
    tmpargument = 0
  Else
    tmpargument = 1
  SendMessage

//Use shield and attack
IfTargetIsOnHatedTeam
  IfFacingTarget
    tmpx = targetdistance
    tmpy = 130
    IfXIsLessThanY
      IfHoldingMeleeWeapon
        PressLatchButton
      IfTargetIsAttacking
        tmpargument = [SHIE]
        IfHoldingItemID
          PressLatchButton


//Main AI loop
IfTimeOut
  tmpargument = rand & 31 + 15
  SetTime

  tmpx = selflife
  tmpy = 1500           //Less than 6 hp
  IfXIsLessThanY
    tmpargument = 1
  Else
    tmpargument = 0
  ChangeArmor

  //Standing at spawn (which is randomized)
  IfStateIs0
    SetTurnModeToVelocity
    SetTargetToWideEnemy
      tmpargument = 1
      SetState
    tmpargument = 0
    GetXY
    ClearWaypoints
    AddWaypoint

  //Combat
  IfStateIs1
    SetTargetToWideEnemy
      tmpx = targetx
      tmpy = targety
      tmpdistance = 1500
      tmpturn = targetturnto
      ClearWaypoints
      AddWaypoint
    Else
      tmpargument = 0
      SetState                  //No enemies around anymore

  //Resume talking
  IfStateIs2
    tmpargument = 0
    SetState
  IfStateIs4
    tmpargument = 3
    SetState

End
