//The different states
//0: Wander around as Shopkeeper
//1: Combat
//selforder = item bought/sold price
//selfcounter = transaction type (0, 1 or 2)
//tmpdistance = chance to get detected when stealing
//Content set to 1 means spawn particles (Stakes that marks shop)

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

//------------------------------------------------------------------------------
//Check if we mark the shop area with particles
IfSpawned
  tmpargument = 1
  IfArmorIs			//Mark secret shop as a shop... eh
    tmpargument = passage
    AddShopPassage
  IfContentIs
    tmpargument = passage	//Yep, lets setup shop..
    AddShopPassage

    // Make the border
    tmpargument = 2
    tmpdistance = 0

    // Top
    tmpx = selfx - 122
    tmpy = selfy - 324
    SpawnExactParticle
    tmpx = tmpx + 88
    SpawnExactParticle
    tmpx = tmpx + 88
    SpawnExactParticle
    tmpx = tmpx + 88
    SpawnExactParticle

    // Bottom
    tmpy = selfy + 324
    SpawnExactParticle
    tmpx = tmpx - 88
    SpawnExactParticle
    tmpx = tmpx - 88
    SpawnExactParticle
    tmpx = tmpx - 88
    SpawnExactParticle

    // Left
    tmpx = selfx - 304
    tmpy = selfy - 102
    SpawnExactParticle
    tmpy = tmpy + 88
    SpawnExactParticle
    tmpy = tmpy + 88
    SpawnExactParticle
    tmpy = tmpy + 88
    SpawnExactParticle

    // Right
    tmpx = selfx + 304
    SpawnExactParticle
    tmpy = tmpy - 88
    SpawnExactParticle
    tmpy = tmpy - 88
    SpawnExactParticle
    tmpy = tmpy - 88
    SpawnExactParticle
    
//------------------------------------------------------------------------------
// Get mean if characters wanna be bad
IfAttacked
  CallForHelp
  IfTargetIsAPlayer
    tmpargument = TEAME
    TargetJoinTeam
  tmpargument = 1	//Combat stance
  SetState

IfCalledForHelp
  tmpargument = 1	//Combat stance
  SetState

IfKilled
  tmpargument = TEAME
  TargetJoinTeam
  
  //Make chests lootable
  tmpdistance = [LOOT]
  tmpargument = 983  	// The unique code for a looted shop
  OrderSpecialID
  CallForHelp			//Warn friends

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

//------------------------------------------------------------------------------
//Setup the shops with all the goods
IfSpawned

  //SETUP DUNLAR BLACKSMITH SHOP
  tmpx = passage
  tmpy = 1
  IfXIsEqualToY
    tmpdistance = 0
    tmpx = selfx - 128
    tmpy = selfy - 128
    tmpturn = rand

//ITEM 1
    tmpargument = rand % 13
    SetState
    IfStateIs0			//Spear
      tmpargument = 75
    IfStateIs1			//Sword
      tmpargument = 57
    IfStateIs2		  	//Lance
      tmpargument = 68
    IfStateIs3 	 		//Broad Sword
      tmpargument = 82
    IfStateIs4			//Scitmitar
      tmpargument = 92
    IfStateIs5		  	//Shield
      tmpargument = 76
    IfStateIs6			//Mace
      tmpargument = 78
    IfStateIs7		 	//Morningstar
      tmpargument = 66
    IfStateIs8		  	//Hatchet
      tmpargument = 77
    IfStateIs9
      tmpargument = 74		//Kite shield (Non griffin)
    IfStateIs10
      tmpargument = 131			//Scythe
    IfStateIs11
      tmpargument = 150			//Quiver
    IfStateIs12
      tmpargument = 139			//Trident
    SpawnExactCharacterXYZ

//ITEM 2
    tmpx = selfx
    tmpturn = rand
    tmpargument = rand % 13
    SetState
    IfStateIs0			//Spear
      tmpargument = 75
    IfStateIs1			//Sword
      tmpargument = 57
    IfStateIs2		  	//Lance
      tmpargument = 68
    IfStateIs3 	 		//Broad Sword
      tmpargument = 82
    IfStateIs4			//Scitmitar
      tmpargument = 92
    IfStateIs5		  	//Shield
      tmpargument = 76
    IfStateIs6			//Mace
      tmpargument = 78
    IfStateIs7		 	//Morningstar
      tmpargument = 66
    IfStateIs8		  	//Hatchet
      tmpargument = 77
    IfStateIs9
      tmpargument = 74		//Kite shield (Non griffin)
    IfStateIs10
      tmpargument = 131			//Scythe
    IfStateIs11
      tmpargument = 150			//Quiver
    IfStateIs12
      tmpargument = 139			//Trident
    SpawnExactCharacterXYZ

//ITEM 3
    tmpx = selfx + 128
    tmpturn = rand
    tmpargument = rand % 13
    SetState
    IfStateIs0			//Spear
      tmpargument = 75
    IfStateIs1			//Sword
      tmpargument = 57
    IfStateIs2		  	//Lance
      tmpargument = 68
    IfStateIs3 	 		//Broad Sword
      tmpargument = 82
    IfStateIs4			//Scitmitar
      tmpargument = 92
    IfStateIs5		  	//Shield
      tmpargument = 76
    IfStateIs6			//Mace
      tmpargument = 78
    IfStateIs7		 	//Morningstar
      tmpargument = 66
    IfStateIs8		  	//Hatchet
      tmpargument = 77
    IfStateIs9
      tmpargument = 74		//Kite shield (Non griffin)
    IfStateIs10
      tmpargument = 131			//Scythe
    IfStateIs11
      tmpargument = 150			//Quiver
    IfStateIs12
      tmpargument = 139			//Trident
    SpawnExactCharacterXYZ

//ITEM 4
    tmpy = selfy
    tmpturn = rand
    tmpargument = rand % 13
    SetState
    IfStateIs0			//Spear
      tmpargument = 75
    IfStateIs1			//Sword
      tmpargument = 57
    IfStateIs2		  	//Lance
      tmpargument = 68
    IfStateIs3 	 		//Broad Sword
      tmpargument = 82
    IfStateIs4			//Scitmitar
      tmpargument = 92
    IfStateIs5		  	//Shield
      tmpargument = 76
    IfStateIs6			//Mace
      tmpargument = 78
    IfStateIs7		 	//Morningstar
      tmpargument = 66
    IfStateIs8		  	//Hatchet
      tmpargument = 77
    IfStateIs9
      tmpargument = 74		//Kite shield (Non griffin)
    IfStateIs10
      tmpargument = 131			//Scythe
    IfStateIs11
      tmpargument = 150			//Quiver
    IfStateIs12
      tmpargument = 139			//Trident
    SpawnExactCharacterXYZ

//ITEM 5
    tmpx = selfx - 128
    tmpturn = rand
    tmpargument = rand % 13
    SetState
    IfStateIs0			//Spear
      tmpargument = 75
    IfStateIs1			//Sword
      tmpargument = 57
    IfStateIs2		  	//Lance
      tmpargument = 68
    IfStateIs3 	 		//Broad Sword
      tmpargument = 82
    IfStateIs4			//Scitmitar
      tmpargument = 92
    IfStateIs5		  	//Shield
      tmpargument = 76
    IfStateIs6			//Mace
      tmpargument = 78
    IfStateIs7		 	//Morningstar
      tmpargument = 66
    IfStateIs8		  	//Hatchet
      tmpargument = 77
    IfStateIs9
      tmpargument = 74		//Kite shield (Non griffin)
    IfStateIs10
      tmpargument = 131			//Scythe
    IfStateIs11
      tmpargument = 150			//Quiver
    IfStateIs12
      tmpargument = 139			//Trident
    SpawnExactCharacterXYZ

//ITEM 6
    tmpy = selfy + 128
    tmpturn = rand
    tmpargument = rand % 13
    SetState
    IfStateIs0			//Spear
      tmpargument = 75
    IfStateIs1			//Sword
      tmpargument = 57
    IfStateIs2		  	//Lance
      tmpargument = 68
    IfStateIs3 	 		//Broad Sword
      tmpargument = 82
    IfStateIs4			//Scitmitar
      tmpargument = 92
    IfStateIs5		  	//Shield
      tmpargument = 76
    IfStateIs6			//Mace
      tmpargument = 78
    IfStateIs7		 	//Morningstar
      tmpargument = 66
    IfStateIs8		  	//Hatchet
      tmpargument = 77
    IfStateIs9
      tmpargument = 74		//Kite shield (Non griffin)
    IfStateIs10
      tmpargument = 131			//Scythe
    IfStateIs11
      tmpargument = 150			//Quiver
    IfStateIs12
      tmpargument = 139			//Trident
    SpawnExactCharacterXYZ

//ITEM 7
    tmpx = selfx
    tmpturn = rand
    tmpargument = rand % 13
    SetState
    IfStateIs0			//Spear
      tmpargument = 75
    IfStateIs1			//Sword
      tmpargument = 57
    IfStateIs2		  	//Lance
      tmpargument = 68
    IfStateIs3 	 		//Broad Sword
      tmpargument = 82
    IfStateIs4			//Scitmitar
      tmpargument = 92
    IfStateIs5		  	//Shield
      tmpargument = 76
    IfStateIs6			//Mace
      tmpargument = 78
    IfStateIs7		 	//Morningstar
      tmpargument = 66
    IfStateIs8		  	//Hatchet
      tmpargument = 77
    IfStateIs9
      tmpargument = 74		//Kite shield (Non griffin)
    IfStateIs10
      tmpargument = 131			//Scythe
    IfStateIs11
      tmpargument = 150			//Quiver
    IfStateIs12
      tmpargument = 139			//Trident
    SpawnExactCharacterXYZ

//ITEM 8
    tmpx = selfx + 128
    tmpturn = rand
    tmpargument = rand % 13
    SetState
    IfStateIs0			//Spear
      tmpargument = 75
    IfStateIs1			//Sword
      tmpargument = 57
    IfStateIs2		  	//Lance
      tmpargument = 68
    IfStateIs3 	 		//Broad Sword
      tmpargument = 82
    IfStateIs4			//Scitmitar
      tmpargument = 92
    IfStateIs5		  	//Shield
      tmpargument = 76
    IfStateIs6			//Mace
      tmpargument = 78
    IfStateIs7		 	//Morningstar
      tmpargument = 66
    IfStateIs8		  	//Hatchet
      tmpargument = 77
    IfStateIs9
      tmpargument = 74		//Kite shield (Non griffin)
    IfStateIs10
      tmpargument = 131			//Scythe
    IfStateIs11
      tmpargument = 150			//Quiver
    IfStateIs12
      tmpargument = 139			//Trident
    SpawnExactCharacterXYZ

//EXTRA ITEM (Special item)
    tmpx = selfx
    tmpy = selfy
    tmpargument = rand % 6
    SetState
    IfStateIs0			//Magic lance
      tmpargument = 102
    IfStateIs1			//Magic crossbow
      tmpargument = 103
    IfStateIs2			//Titans maul
      tmpargument = 120
    IfStateIs3			//Weapon masters sword
      tmpargument = 122
    IfStateIs4			//Frost Sabre
      tmpargument = 148
    IfStateIs5			//Magic shield
      tmpargument = 84
    SpawnExactCharacterXYZ

  tmpargument = 0
  SetState

  //SETUP SECRET SHOPPE
  tmpargument = 1
  IfArmorIs

//ITEM 1
    tmpx = selfx
    tmpy = selfy
    tmpturn = rand
    tmpargument = rand % 11
    SetState
    IfStateIs0			//Magic Lance
      tmpargument = 102
    IfStateIs1			//Fireballwand
      tmpargument = 73
    IfStateIs2			//Magic shield
      tmpargument = 84
    IfStateIs3			//Fire Bow
      tmpargument = 87
    IfStateIs4			//Frost Sabre
      tmpargument = 148
    IfStateIs5			//Magic crossbow
      tmpargument = 103
    IfStateIs6			//Levitation Shoes
      tmpargument = 147
    IfStateIs7			//Orbstorm wand
      tmpargument = 117
    IfStateIs8			//Titans maul
      tmpargument = 120
    IfStateIs9			//Weapon masters sword
      tmpargument = 122
    IfStateIs10			//Elixir of Life
      tmpargument = 130
    SpawnExactCharacterXYZ

//ITEM 2
    tmpx = selfx
    tmpy = selfy + 175
    tmpturn = rand
    tmpargument = rand % 11
    SetState
    IfStateIs0			//Magic Lance
      tmpargument = 102
    IfStateIs1			//Fireballwand
      tmpargument = 73
    IfStateIs2			//Magic shield
      tmpargument = 84
    IfStateIs3			//Fire Bow
      tmpargument = 87
    IfStateIs4			//Frost Sabre
      tmpargument = 148
    IfStateIs5			//Magic crossbow
      tmpargument = 103
    IfStateIs6			//Levitation Shoes
      tmpargument = 147
    IfStateIs7			//Orbstorm wand
      tmpargument = 117
    IfStateIs8			//Titans maul
      tmpargument = 120
    IfStateIs9			//Weapon masters sword
      tmpargument = 122
    IfStateIs10			//Elixir of Life
      tmpargument = 130
    SpawnExactCharacterXYZ

//ITEM 3
    tmpx = selfx
    tmpy = selfy - 174
    tmpturn = rand
    tmpargument = rand % 11
    SetState
    IfStateIs0			//Magic Lance
      tmpargument = 102
    IfStateIs1			//Fireballwand
      tmpargument = 73
    IfStateIs2			//Magic shield
      tmpargument = 84
    IfStateIs3			//Fire Bow
      tmpargument = 87
    IfStateIs4			//Frost Sabre
      tmpargument = 148
    IfStateIs5			//Magic crossbow
      tmpargument = 103
    IfStateIs6			//Levitation Shoes
      tmpargument = 147
    IfStateIs7			//Orbstorm wand
      tmpargument = 117
    IfStateIs8			//Titans maul
      tmpargument = 120
    IfStateIs9			//Weapon masters sword
      tmpargument = 122
    IfStateIs10			//Elixir of Life
      tmpargument = 130
    SpawnExactCharacterXYZ
  tmpargument = 0
  SetState

  //SETUP SHADY ROGUE GOODS
  tmpargument = 2
  IfArmorIs
//ITEM 0
    tmpturn = rand
    tmpdistance = 0
    tmpx = selfx
    tmpy = selfy
    tmpargument = 89		//Knife
    SpawnExactCharacterXYZ
//ITEM 1
    tmpturn = rand
    tmpx = selfx - 178
    tmpy = selfy - 178
    tmpargument = 67		//Cinquanda
    SpawnExactCharacterXYZ
//ITEM 2
    tmpx = selfx
    tmpturn = rand
    tmpargument = 101		//Ninjastars
    SpawnExactCharacterXYZ
//ITEM 3
    tmpx = selfx + 178
    tmpturn = rand
    tmpargument = 107		//Poison stiletto
    SpawnExactCharacterXYZ
//ITEM 4
    tmpy = selfy
    tmpturn = rand
    tmpargument = 65		//Claw
    SpawnExactCharacterXYZ
//ITEM 5
    tmpx = selfx - 178
    tmpturn = rand
    tmpargument = 85		//Bear Trap
    SpawnExactCharacterXYZ
//ITEM 6
    tmpy = selfy + 178
    tmpturn = rand
    tmpargument = 85		//Bear Trap
    SpawnExactCharacterXYZ
//ITEM 7
    tmpx = selfx
    tmpturn = rand
    tmpargument = 62		//Crossbow
    SpawnExactCharacterXYZ
//ITEM 8
    tmpx = selfx + 178
    tmpturn = rand
    tmpargument = 101		//Ninjastars
    SpawnExactCharacterXYZ
//ITEM 9
    tmpturn = rand
    tmpdistance = 0
    tmpx = selfx + 56
    tmpy = selfy - 56
    tmpargument = 128		//Poison Katana
    SpawnExactCharacterXYZ
//ITEM 10
    tmpturn = rand
    tmpdistance = 0
    tmpx = selfx - 128
    tmpy = selfy - 56
    tmpargument = 150		//Ammo Quiver
    SpawnExactCharacterXYZ

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

  //SHOPKEEPER
  IfStateIs0				//Wander around
    IfHoldingMeleeWeapon
      tmpargument = LATCHPACKRIGHT	//Unequip weapon
      PressLatchButton
    tmpx = rand & 255 - 128 + selfspawnx
    tmpy = rand & 255 - 128 + selfspawny
    ClearWaypoints
    AddWaypoint
    SetTargetToNearbyEnemy		//Enemy enters shop
      tmpargument = 1			//Go combat
      SetState

  //COMBAT
  IfStateIs1
    SetTargetToWideEnemy		//Get in combat position
      tmpx = targetx
      tmpy = targety
      tmpturn = targetturnto
      Compass
      ClearWaypoints
      AddWaypoint

      IfHoldingMeleeWeapon		//Attack?
        tmpx = targetdistance
        tmpy = 200
        IfXIsLessThanY
          IfTargetIsAlive
            IfTargetIsOnHatedTeam
              tmpargument = LATCHRIGHT
              PressLatchButton
      Else
        tmpargument = LATCHPACKRIGHT	//No weapon equipped!
        PressLatchButton
    Else				//No enemies around
      tmpargument = 0
      SetState

  tmpargument = rand & 31 + 15
  SetTime

//------------------------------------------------------------------------------
//Handle death by sending a message and other stuff
IfKilled
  JoinEvilTeam
  tmpargument = 3
  tmpdistance = rand & 1027 + 11000
  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

//------------------------------------------------------------------------------
// Handle being attacked by blocking or countering or running away
IfAttacked
  SetTargetToWhoeverAttacked
  IfTargetIsOnSameTeam
    tmpargument = 2
    tmpdistance = rand & 1027 + 11000
    PlaySound
    tmpargument = MESSAGEOUCH
    SendMessageNear
    JoinEvilTeam
  Else
    tmpargument = rand & 1 + 4
    tmpdistance = rand & 1027 + 11000
    PlaySound

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