//The different states
//0: Shopkeeper
//1: In combat

// Show the character's location
tmpx = selfx
tmpy = selfy
tmpargument = YELLOW
ShowBlipXY

//------------------------------------------------------------------------------
// 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
  
//------------------------------------------------------------------------------
// Get mean if characters wanna be bad
IfAttacked			//Sombody dares to attack zambini?!
  SetTargetToWhoeverAttacked
  IfStateIs0
    IfTargetIsAPlayer
      tmpargument = MESSAGEOUCH
      SendMessageNear

  IfTargetIsOnHatedTeam
    CallForHelp
    BecomeLeader

  tmpargument = TEAME
  TargetJoinTeam			//Give them hell
  
  tmpargument = 1		//Enter combat
  SetState
  
IfCalledForHelp			//Somebody else is under attack
  tmpargument = 1		//Enter combat
  SetState

IfSomeoneIsStealing     //Someone is stealing from our shop! ><
  CallForHelp           //Call the guards!
  tmpargument = TEAME
  TargetJoinTeam
  tmpargument = 1
  SetState                  //combat mode

//------------------------------------------------------------------------------
//COMBAT AI

IfTimeOut
  tmpargument = rand % 21 + 80
  SetTime

//Drink mana potion?
  SetTargetToSelf
  tmpx = 1000
  tmpy = targetmana
  IfXIsMoreThanY	//Low mana
    tmpx = rand & 100
    tmpy = 90		//10% chance each round
    IfXIsMoreThanY
      tmpargument = 8
      tmpdistance = rand % 9000 + 3000
      PlaySound		//Make potion sound
      tmpargument = 14
      SendMessageNear		//Tell the players what happened
      tmpargument = rand % 2000	+ 3000 //A lot of mana
      PumpTarget

  IfStateIs0			//State 0, Shopkeeper
    tmpx = rand & 255 - 128 + selfspawnx
    tmpy = rand & 255 - 128 + selfspawny
    ClearWaypoints
    AddWaypoint
    tmpargument = 0  //Become Talkative again
    SetContent

  IfStateIs1			//Combat state
    SetTargetToWideEnemy	//Find something to kill
      tmpx = targetdistance
      tmpy = 400
      IfXIsMoreThanY		//Move closer?
        tmpx = targetx
        tmpy = targety
        tmpturn = targetturnto
        Compass
        ClearWaypoints
        AddWaypoint
        tmpargument = 40
        SetTime
      Else			//Close enough - combat stance
        tmpx = selfx
        tmpy = selfy
        tmpturn = targetturnto
        Compass
        ClearWaypoints
        AddWaypoint
    Else			//No enemies in sight, wander
      tmpx = rand & 255 - 128 + selfspawnx
      tmpy = rand & 255 - 128 + selfspawny
      ClearWaypoints
      AddWaypoint

//Cast lightning bolt? (Part of SPELL CASTING AI)
    SetTargetToNearbyEnemy
      tmpx = rand & 1000
      tmpy = 70			//30% chance
      IfXIsMoreThanY
        SetOldTarget
        SetTargetToSelf
        tmpx = targetmana
        tmpy = 738			//3 points of mana
        IfXIsMoreThanY		//Enough mana to cast spell?
          tmpargument = 738
          CostTargetMana
          SetTargetToOldTarget	//Hit the enemy, not yourself
          tmpargument = 7
          tmpx = targetx
          tmpy = targety
          tmpdistance = 0
          SpawnExactParticle	//Strike him down!

//Cast frost nova? (Part of SPELL CASTING AI)
      tmpx = targetdistance
      tmpy = 400
      IfXIsLessThanY			//Is he close enough?
        tmpx = rand & 100
        tmpy = 20 			//80% chance to do it
          IfXIsMoreThanY
            SetTargetToSelf
            tmpx = targetmana
            tmpy = 512
            IfXIsMoreThanY		//Enough mana to cast spell?
              tmpargument = 5
              tmpx = selfx
              tmpy = selfy
              SpawnExactParticle	//The nova is unleashed
              tmpargument = 512
              CostTargetMana	//Cost 2 mana points

//Attack with staff
IfBumped
  SetTargetToWhoeverBumped
  IfTargetIsOnHatedTeam
    IfFacingTarget
      tmpargument = LATCHRIGHT
      PressLatchButton

//------------------------------------------------------------------------------
//SPELL CASTING AI

//Launch firebolts
IfFacingTarget
  IfTargetIsOnHatedTeam
    SetTargetToSelf
    tmpx = targetmana
    tmpy = 256			//Do we have enough mana?
    IfXIsMoreThanY
      tmpargument = LATCHLEFT
      PressLatchButton

//Unleash frost nova
IfAttacked			//Unleash when attacked
  SetTargetToWhoeverAttacked
  IfTargetIsOnHatedTeam
    tmpx = targetdistance	//Is he close enough
    tmpy = 300
    IfXIsLessThanY
      tmpx = rand & 100		//20% chance to do it
      tmpy = 80
      IfXIsMoreThanY
        SetTargetToSelf
        tmpx = targetmana
        tmpy = 512
        IfXIsMoreThanY		//Enough mana to cast spell?
          tmpargument = 5
          tmpx = selfx
          tmpy = selfy
          SpawnExactParticle	//The nova is unleashed
          tmpargument = 512
          CostTargetMana	//Cost 2 mana points
IfBumped			//Unleash when bumped
  IfTargetIsOnHatedTeam
    tmpx = rand & 100
    tmpy = 30 			//70% chance to do it
      IfXIsMoreThanY
        SetTargetToSelf
        tmpx = targetmana
        tmpy = 512
        IfXIsMoreThanY		//Enough mana to cast spell?
          tmpargument = 5
          tmpx = selfx
          tmpy = selfy
          SpawnExactParticle	//The nova is unleashed
          tmpargument = 512
          CostTargetMana	//Cost 2 mana points

//Firebolt manacost
IfUsed
  SetTargetToSelf
  tmpargument = 256		//1 mana point
  CostTargetMana

//------------------------------------------------------------------------------
//Spawn sparklies around wizard (Just for fancy)
SetTargetToSelf
tmpx = targetx
tmpy = targety
tmpdistance = targetz
tmpargument = 3
SpawnExactParticle

//------------------------------------------------------------------------------
// 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

//Talk AI
IfBumped
  SetTargetToWhoeverBumped
  IfTargetIsAPlayer
    tmpargument = 0
    IfContentIs				//Prevent loops
      IfStateIs0			//Only if friendly
        tmpx = 40			//40% chance of talking
        tmpy = rand % 100
        IfXIsMoreThanY
          tmpargument = rand % 5 + 9
          tmpdistance = rand & 1027 + 11000
          PlaySound			//Sound effect
          SendMessageNear		//and message
          tmpargument = 1		//Prevent looping
          SetContent

//------------------------------------------------------------------------------
// ZZ> Handle death by sending a message and other stuff
IfKilled
  CallForHelp			//Warn friends
  JoinEvilTeam
  tmpargument = 3
  PlaySound
  tmpargument = MESSAGEDEATH
  IfTargetIsOnSameTeam
    tmpargument = MESSAGEFRAG
    IfTargetIsSelf
      tmpargument = MESSAGEACCIDENT
  SendMessage

  // Drop goodies
  tmpargument = 65535
  DropMoney

  // Make the character body
  tmpargument = 45
  SetBumpHeight

  //Make chests lootable
  tmpdistance = [SHOP]
  tmpargument = 983  // The unique code for a looted shop
  OrderSpecialID

//------------------------------------------------------------------------------
// ZZ> Handle being attacked by blocking or countering or running away
IfAttacked
  SetTargetToWhoeverAttacked
  IfTargetIsOnSameTeam
    tmpargument = rand & 1 + 5
    PlaySound
    JoinEvilTeam
    tmpargument = 1		//Enter combat
    SetState
  Else
    tmpargument = rand & 1 + 4
    PlaySound

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