// Get mean if characters wanna be bad
IfAttacked
  SetTargetToWhoeverAttacked
  IfTargetIsAPlayer
    CallForHelp
    tmpargument = TEAME       //Team EVIL
    TargetJoinTeam
  tmpargument = rand & 1
  PlaySound
IfKilled
  SetTargetToWhoeverAttacked
  CallForHelp
  tmpargument = TEAME   //Team EVIL
  TargetJoinTeam
IfSomeoneIsStealing     //Someone is stealing from our shop! ><
  CallForHelp           //Call the guards!
  tmpargument = TEAME       //Team EVIL
  TargetJoinTeam

//------------------------------------------------------------------------------
// ZZ> Setup shop...
IfSpawned
  tmpargument = passage
  AddShopPassage

  // Make the border
  tmpargument = 2
  tmpdistance = 0

  // Top
  tmpx = selfx - 192
  tmpy = selfy - 384
  SpawnExactParticle
  tmpx = tmpx + 128
  SpawnExactParticle
  tmpx = tmpx + 128
  SpawnExactParticle
  tmpx = tmpx + 128
  SpawnExactParticle

  // Bottom
  tmpy = selfy + 384
  SpawnExactParticle
  tmpx = tmpx - 128
  SpawnExactParticle
  tmpx = tmpx - 128
  SpawnExactParticle
  tmpx = tmpx - 128
  SpawnExactParticle

  // Left
  tmpx = selfx - 384
  tmpy = selfy - 192
  SpawnExactParticle
  tmpy = tmpy + 128
  SpawnExactParticle
  tmpy = tmpy + 128
  SpawnExactParticle
  tmpy = tmpy + 128
  SpawnExactParticle

  // Right
  tmpx = selfx + 384
  SpawnExactParticle
  tmpy = tmpy - 128
  SpawnExactParticle
  tmpy = tmpy - 128
  SpawnExactParticle
  tmpy = tmpy - 128
  SpawnExactParticle

  // Show the area
  //tmpdistance = 255
  //FlashPassage

//------------------------------------------------------------------------------
// ZZ> Handle orders ( purchases )
IfOrdered
  // Counter specifies transaction ( 0, 1, or 2 )
  // Order specifies price
  tmpx = selforder
  tmpy = 0
  IfXIsEqualToY
    tmpx = selfcounter
    tmpy = 0
    IfXIsEqualToY
      // I'm not payin' for that!
      tmpargument = 8
      SendMessageNear
  Else
    // Buy/Sell
    tmpx = selfcounter
    tmpy = 3
    IfXIsLessThanY
      tmpy = 0 - 1
      IfXIsMoreThanY
        tmpargument = tmpx + 5
        tmpx = selforder
        SendMessageNear

//------------------------------------------------------------------------------
// ZZ> Move around...  Start at state 0
IfTimeOut
  IfStateIs0
    SetTurnModeToVelocity
    tmpx = rand & 255 - 128 + selfspawnx
    tmpy = rand & 255 - 128 + selfspawny
    ClearWaypoints
    AddWaypoint
    SetTargetToNearbyEnemy
      tmpargument = 1
      SetState
  Else
    SetTargetToWideEnemy
      SetTurnModeToWatchTarget
      tmpx = rand & 511 - 256 + targetx
      tmpy = rand & 511 - 256 + targety
      tmpdistance = - 128*4
      tmpturn = targetturnto
      Compass
      ClearWaypoints
      AddWaypoint
    Else
      tmpargument = 0
      SetState
  tmpargument = rand & 31 + 15
  SetTime
  
  //Cast protect spell?
  tmpargument = 0
  GetXY
  IfXIsEqualToY
    tmpdistance = 128*20
    SetTargetToDistantEnemy
      tmpargument = 3
      SetState

//Charge spells
IfStateIs1
  tmpargument = 0
  IfContentIs
    tmpx = selfmana
    tmpy = 256*3          //Only start charging if enough mana
    IfXIsMoreThanY
      tmpargument = 1
      SetContent
  Else
    tmpargument = selfcontent + 1
    SetContent
    tmpx = selfcontent
    tmpy = 80
    IfXIsLessThanY
      tmpargument = LATCHRIGHT
      PressLatchButton
    Else
      tmpargument = 0
      SetContent
IfStateIs3
  tmpargument = selfcontent + 1
  SetContent
  tmpx = selfcontent
  tmpy = 200
  IfXIsLessThanY
    tmpargument = LATCHLEFT
    PressLatchButton
  Else
    tmpargument = 0
    SetContent
    SetState
    tmpx = 1
    SetXY

//------------------------------------------------------------------------------
// ZZ> Handle death by sending a message and other stuff
IfKilled

  //Death sound
  tmpargument = 2
  PlaySound
  tmpargument = MESSAGEDEATH
  IfTargetIsOnSameTeam
    tmpargument = MESSAGEFRAG
    IfTargetIsSelf
      tmpargument = MESSAGEACCIDENT
  SendMessage

  // Drop goodies
  tmpargument = selfmoney
  DropMoney

  // Make the character body
  tmpargument = 45
  SetBumpHeight

//------------------------------------------------------------------------------
End
//------------------------------------------------------------------------------
