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

  tmpx = rand % 100
  tmpy = 40
  IfXIsMoreThanY        //There is only a 40% chance the shop will actually be here
    GoPoof
  Else
    

    //Make it a shop
    tmpargument = passage
    AddShopPassage

    Sneak				//Dont run around
    tmpargument = STATEGUARD
    SetState
    
    //Spawn the shop goods
    tmpargument = 106          //flask'o sand
    tmpx = selfx - 128
    tmpy = selfy + 100
    tmpdistance = selfz
    tmpturn = rand
    SpawnExactCharacterXYZ
    
    tmpargument = 106          //flask'o sand
    tmpx = selfx - 64
    tmpy = selfy - 100
    tmpdistance = selfz
    tmpturn = rand
    SpawnExactCharacterXYZ
    
    tmpargument = 106          //flask'o sand
    tmpx = selfx - 64
    tmpy = selfy + 100
    tmpdistance = selfz
    tmpturn = 0
    SpawnExactCharacterXYZ
    
    tmpargument = 106          //flask'o sand
    tmpx = selfx - 128
    tmpy = selfy - 100
    tmpdistance = selfz
    tmpturn = rand
    SpawnExactCharacterXYZ
    
    tmpargument = 106          //flask'o sand
    tmpx = selfx - 64
    tmpy = selfy
    tmpdistance = selfz
    tmpturn = rand
    SpawnExactCharacterXYZ

    tmpargument = 106          //flask'o sand  
    tmpx = selfx - 128
    tmpy = selfy
    tmpdistance = selfz
    tmpturn = rand
    SpawnExactCharacterXYZ
  
  
//------------------------------------------------------------------------------
IfHealed
  tmpargument = 1
  SendMessageNear

//------------------------------------------------------------------------------
// Get mean if characters wanna be bad
IfAttacked
  Run
  SetTargetToWhoeverAttacked
  IfTargetIsAPlayer
    CallForHelp
    tmpargument = 5     //Team Evil
    TargetJoinTeam
    tmpargument = STATECOMBAT
    SetState
  tmpargument = 5
  PlaySound
IfCalledForHelp
  Walk
IfKilled
  CallForHelp			//Warn friends
  tmpargument = 5
  TargetJoinTeam

//------------------------------------------------------------------------------
// ZZ> Handle normal state
IfTimeOut
  tmpargument = 50
  SetTime

  //WAIT AND TALK
  IfStateIsGuard
    SetTargetToNearbyEnemy		//Enemy near, go combat mode
      tmpargument = STATECOMBAT
      SetState
      CallForHelp
    Else
      tmpx = selfspawnx
      tmpy = selfspawny
      ClearWaypoints
      AddWaypoint

      //Reduce that counter
      tmpx = selfcontent
      tmpy = 0
      IfXIsMoreThanY
        tmpargument = selfcontent - 1
        SetContent
      
      //Cry out to attract buyers
      tmpdistance = BLAHFRIENDS
      tmpargument = [HUMA]
      SetTargetToNearestBlahID
        tmpx = targetdistance
        tmpy = 550
        IfXIsLessThanY
          SetTurnModeToWatchTarget
          tmpargument = 0
          IfContentIs
            tmpargument = rand % 3 + 2
            SendMessageNear
            PlaySound
            tmpargument = 6   //Wait 6 seconds before doing again
            SetContent
        Else
          SetTurnModeToVelocity
          
  //Run away in panic
  IfStateIsCombat
    Run
    SetTargetToNearbyEnemy
      CallForHelp

    tmpx = rand % 3000 + selfx - 1500
    tmpy = rand % 3000 + selfy - 1500
    ClearWaypoints
    AddWaypoint
    tmpargument = 75
    SetTime
    
    //Return to shopkeeping mode if everything seems normal again
    SetTargetToNearestEnemy
      tmpx = targetdistance
      tmpy = 128*16
      IfXIsMoreThanY
        tmpargument = STATEGUARD
        SetState        
    

//------------------------------------------------------------------------------
//What to do if bumped
IfBumped
  IfStateIsGuard
    SetTargetToWhoeverBumped
    tmpx = selfcontent
    tmpy = 6
    IfXIsLessThanY
      tmpargument = rand % 3 + 2
      SendMessageNear
      PlaySound
      tmpargument = 10
      SetContent

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