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

  //Make it a shop
  tmpargument = passage
  AddShopPassage

  Sneak				//Dont run around
  tmpargument = STATEGUARD
  SetState
  
  //Place the carpet
  DropItems
  
  
//------------------------------------------------------------------------------
IfHealed
  tmpargument = 1
  SendMessageNear

//------------------------------------------------------------------------------
// Get mean if characters wanna be bad
IfAttacked
  Run
  SetTargetToWhoeverAttacked
  IfTargetIsAPlayer
    CallForHelp
    tmpargument = TEAME     //Team Evil
    TargetJoinTeam
    tmpargument = STATECOMBAT
    SetState
  tmpargument = 5
  PlaySound
IfKilled
  CallForHelp			//Warn friends
  tmpargument = TEAME
  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
//------------------------------------------------------------------------------
