//Show where you are on the map
tmpx = selfx
tmpy = selfy
tmpargument = YELLOW
ShowBlipXY

//------------------------------------------------------------------------------
//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 = 5
      SendMessageNear
  Else
    // Buy/Sell
    tmpx = selfcounter
    tmpy = 3
    IfXIsLessThanY
      tmpy = 0 - 1
      IfXIsMoreThanY
        tmpargument = tmpx + 2
        tmpx = selforder
        SendMessageNear
        
//------------------------------------------------------------------------------
IfSpawned

  //Make it a shop
  tmpargument = passage
  AddShopPassage
  tmpargument = STATEGUARD
  SetState
  
  // Make the border
  tmpargument = 3
  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
  
  //Prepeare the random inventory list
  tmpy = 0
  
  //Slot 0
  tmpargument = 0   
  tmpx = 79         //Rune of ressurection
  SetXY
  
  //Slot 1
  tmpargument = 1   
  tmpx = 85         //Rune of clairvoyance
  SetXY
  
  //Slot 2
  tmpargument = 2   
  tmpx = 88         //Rune of Confusion
  SetXY
  
  //Slot 3
  tmpargument = 3   
  tmpx = 112         //Rune of Syphon Mana
  SetXY
  
  //Slot 4
  tmpargument = 4   
  tmpx = 122         //Rune of Combat Blessing
  SetXY
  
  //Slot 5
  tmpargument = 5   
  tmpx = 124         //Rune of Regeneration
  SetXY
  
  //Slot 6
  tmpargument = 6   
  tmpx = 125         //Rune of Exorcism
  SetXY
  
  //Slot 7
  tmpargument = 7   
  tmpx = 128         //Rune of Restoration
  SetXY
  
  //Slot 8
  tmpargument = 8   
  tmpx = 140         //Scroll of truesight
  SetXY
  
  //Slot 9
  tmpargument = 9   
  tmpx = 129         //Rune of Holybolt
  SetXY
  
  //Slot 10
  tmpargument = 10
  tmpx = rand & 1
  tmpy = 0
  IfXIsEqualToY
    tmpx = 81         //50% for The Mana Sword
  Else
    tmpx = 131        //and 50% for Mallet
  SetXY

  //Slot 11
  tmpargument = 11  
  tmpx = 158         //Ring of Protection
  SetXY

  //Slot 12
  tmpargument = 12  
  tmpx = 78         //Kite Shield
  SetXY

  //Slot 13
  tmpargument = 13  
  tmpx = 132         //Mallet
  SetXY

  //Slot 14
  tmpargument = 14
  tmpx = 46         //Spiked Mace
  SetXY
  
  // Add some random goodies
  tmpargument = rand % 15
  GetXY
  tmpargument = tmpx
  tmpdistance = selfz
  tmpx = selfx - 128
  tmpy = selfy - 128
  tmpturn = rand
  SpawnExactCharacterXYZ

  tmpargument = rand % 15
  GetXY
  tmpargument = tmpx
  tmpx = selfx
  tmpy = selfy - 128
  tmpturn = rand
  SpawnExactCharacterXYZ

  tmpargument = rand % 15
  GetXY
  tmpargument = tmpx
  tmpx = selfx + 128
  tmpy = selfy - 128
  tmpturn = rand
  SpawnExactCharacterXYZ

  tmpargument = rand % 15
  GetXY
  tmpargument = tmpx
  tmpx = selfx + 128
  tmpy = selfy
  tmpturn = rand
  SpawnExactCharacterXYZ

  tmpargument = rand % 15
  GetXY
  tmpargument = tmpx
  tmpx = selfx - 128
  tmpy = selfy
  tmpturn = rand
  SpawnExactCharacterXYZ

  tmpargument = rand % 15
  GetXY
  tmpargument = tmpx
  tmpx = selfx - 128
  tmpy = selfy + 128
  tmpturn = rand
  SpawnExactCharacterXYZ

  tmpargument = rand % 15
  GetXY
  tmpargument = tmpx
  tmpx = selfx
  tmpy = selfy + 128
  tmpturn = rand
  SpawnExactCharacterXYZ

  tmpargument = rand % 15
  GetXY
  tmpargument = tmpx
  tmpx = selfx + 128
  tmpy = selfy + 128
  tmpturn = rand
  SpawnExactCharacterXYZ
   
//------------------------------------------------------------------------------
IfHealed
  tmpargument = 1
  SendMessageNear

//------------------------------------------------------------------------------
// Get mean if characters wanna be bad
IfAttacked
  Run
  SetTargetToWhoeverAttacked
  
  //Ouch!
  tmpargument = rand & 1 + 2
  PlaySound
  
  //This priest is prettty forgiving
  tmpargument = 6
  SendMessageNear  
  
//------------------------------------------------------------------------------
// ZZ> Handle normal state
IfTimeOut
  tmpargument = 50
  SetTime

  //Shop keeper mode
  IfStateIsGuard
    Walk
    SetTargetToNearbyEnemy		//Enemy near, go combat mode
      tmpargument = STATECOMBAT
      SetState
      CallForHelp
    Else
      tmpx = rand & 511 + selfspawnx - 256
      tmpy = rand & 511 + selfspawny - 256
      ClearWaypoints
      AddWaypoint
          
  //Run away in panic
  IfStateIsCombat
    Run
    SetTargetToNearbyEnemy
      CallForHelp

    
    //Return to shopkeeping mode if everything seems normal again
    SetTargetToNearestEnemy
      tmpx = targetdistance
      tmpy = 128*8
      IfXIsMoreThanY
        tmpargument = STATEGUARD
        SetState        
    

//------------------------------------------------------------------------------
//What to do if bumped
IfBumped
  SetTargetToWhoeverBumped
  IfTargetIsOnSameTeam
    IfTargetIsHurt
      IfFacingTarget
        tmpargument = LATCHLEFT
        PressLatchButton
        
//------------------------------------------------------------------------------
//Death
IfKilled
  tmpargument = TEAME
  TargetJoinTeam
  
  //kurse them!
  tmpargument = 0
  SendMessageNear
  EnchantTarget
  
  //Make body
  tmpargument = 35
  SetBumpHeight
  
  //Tell the chests what happened
  tmpargument = 983
  tmpdistance = [SHOP]
  OrderSpecialID
  
//------------------------------------------------------------------------------
End
//------------------------------------------------------------------------------
