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

// Get mean if characters wanna be bad
IfAttacked
  SetTargetToWhoeverAttacked
  IfTargetIsAPlayer
    CallForHelp
    JoinEvilTeam
    tmpargument = 1	//Go combat
    SetState
  Else
    SetTargetToOldTarget
IfKilled
  CallForHelp			//Warn friends
IfCalledForHelp
  JoinEvilTeam
IfSomeoneIsStealing     //Someone is stealing from our shop! ><
  CallForHelp           //Call the guards!
  JoinEvilTeam

//------------------------------------------------------------------------------
// Add some random goodies
IfSpawned
  tmpargument = 0
  IfArmorIs			//OLAF JUNK SHOP
    tmpdistance = 0
    tmpx = selfx - 178
    tmpy = selfy - 178
    tmpturn = rand

//ITEM 1
    tmpargument = rand % 13
    SetState
    IfStateIs0			 //Rusty Sword
      tmpargument = 44
    IfStateIs1			 //Torch
      tmpargument = 37
    IfStateIs2			 //Pick
      tmpargument = 48
    IfStateIs3			 //Quarter Staff
      tmpargument = 47
    IfStateIs4			 //Rusty Shield
      tmpargument = 45
    IfStateIs5		         //Poison Potion
      tmpargument = 69
    IfStateIs6		         //Claw
      tmpargument = 65
    IfStateIs7		         //Oil flask
      tmpargument = 64
    IfStateIs8		         //Identify Scroll
      tmpargument = 88
    IfStateIs9		         //Torch
      tmpargument = 37
    IfStateIs10		         //Shovel
      tmpargument = 149
    IfStateIs11		         //Mushroom
      tmpargument = 152
    IfStateIs12		         //Crowbar
      tmpargument = 157
    SpawnExactCharacterXYZ

//ITEM 2
    tmpx = selfx
    tmpturn = rand
    tmpargument = rand % 13
    SetState
    IfStateIs0			 //Rusty Sword
      tmpargument = 44
    IfStateIs1			 //Torch
      tmpargument = 37
    IfStateIs2			 //Pick
      tmpargument = 48
    IfStateIs3			 //Quarter Staff
      tmpargument = 47
    IfStateIs4			 //Rusty Shield
      tmpargument = 45
    IfStateIs5		         //Poison Potion
      tmpargument = 69
    IfStateIs6		         //Claw
      tmpargument = 65
    IfStateIs7		         //Oil flask
      tmpargument = 64
    IfStateIs8		         //Identify Scroll
      tmpargument = 88
    IfStateIs9		         //Torch
      tmpargument = 37
    IfStateIs10		         //Shovel
      tmpargument = 149
    IfStateIs11		         //Mushroom
      tmpargument = 152
    IfStateIs12		         //Crowbar
      tmpargument = 157
    SpawnExactCharacterXYZ

//ITEM 3
    tmpx = selfx + 178
    tmpturn = rand
    tmpargument = rand % 13
    SetState
    IfStateIs0			 //Rusty Sword
      tmpargument = 44
    IfStateIs1			 //Torch
      tmpargument = 37
    IfStateIs2			 //Pick
      tmpargument = 48
    IfStateIs3			 //Quarter Staff
      tmpargument = 47
    IfStateIs4			 //Rusty Shield
      tmpargument = 45
    IfStateIs5		         //Poison Potion
      tmpargument = 69
    IfStateIs6		         //Claw
      tmpargument = 65
    IfStateIs7		         //Oil flask
      tmpargument = 64
    IfStateIs8		         //Identify Scroll
      tmpargument = 88
    IfStateIs9		         //Torch
      tmpargument = 37
    IfStateIs10		         //Shovel
      tmpargument = 149
    IfStateIs11		         //Mushroom
      tmpargument = 152
    IfStateIs12		         //Crowbar
      tmpargument = 157
    SpawnExactCharacterXYZ

//ITEM 4
    tmpy = selfy
    tmpturn = rand
    tmpargument = rand % 13
    SetState
    IfStateIs0			 //Rusty Sword
      tmpargument = 44
    IfStateIs1			 //Torch
      tmpargument = 37
    IfStateIs2			 //Pick
      tmpargument = 48
    IfStateIs3			 //Quarter Staff
      tmpargument = 47
    IfStateIs4			 //Rusty Shield
      tmpargument = 45
    IfStateIs5		         //Poison Potion
      tmpargument = 69
    IfStateIs6		         //Claw
      tmpargument = 65
    IfStateIs7		         //Oil flask
      tmpargument = 64
    IfStateIs8		         //Identify Scroll
      tmpargument = 88
    IfStateIs9		         //Torch
      tmpargument = 37
    IfStateIs10		         //Shovel
      tmpargument = 149
    IfStateIs11		         //Mushroom
      tmpargument = 152
    IfStateIs12		         //Crowbar
      tmpargument = 157
    SpawnExactCharacterXYZ

//ITEM 5
    tmpx = selfx - 178
    tmpturn = rand
    tmpargument = rand % 13
    SetState
    IfStateIs0			 //Rusty Sword
      tmpargument = 44
    IfStateIs1			 //Torch
      tmpargument = 37
    IfStateIs2			 //Pick
      tmpargument = 48
    IfStateIs3			 //Quarter Staff
      tmpargument = 47
    IfStateIs4			 //Rusty Shield
      tmpargument = 45
    IfStateIs5		         //Poison Potion
      tmpargument = 69
    IfStateIs6		         //Claw
      tmpargument = 65
    IfStateIs7		         //Oil flask
      tmpargument = 64
    IfStateIs8		         //Identify Scroll
      tmpargument = 88
    IfStateIs9		         //Torch
      tmpargument = 37
    IfStateIs10		         //Shovel
      tmpargument = 149
    IfStateIs11		         //Mushroom
      tmpargument = 152
    IfStateIs12		         //Crowbar
      tmpargument = 157
    SpawnExactCharacterXYZ

//ITEM 6
    tmpy = selfy + 178
    tmpturn = rand
    tmpargument = rand % 13
    SetState
    IfStateIs0			 //Rusty Sword
      tmpargument = 44
    IfStateIs1			 //Torch
      tmpargument = 37
    IfStateIs2			 //Pick
      tmpargument = 48
    IfStateIs3			 //Quarter Staff
      tmpargument = 47
    IfStateIs4			 //Rusty Shield
      tmpargument = 45
    IfStateIs5		         //Poison Potion
      tmpargument = 69
    IfStateIs6		         //Claw
      tmpargument = 65
    IfStateIs7		         //Oil flask
      tmpargument = 64
    IfStateIs8		         //Identify Scroll
      tmpargument = 88
    IfStateIs9		         //Torch
      tmpargument = 37
    IfStateIs10		         //Shovel
      tmpargument = 149
    IfStateIs11		         //Mushroom
      tmpargument = 152
    IfStateIs12		         //Crowbar
      tmpargument = 157
    SpawnExactCharacterXYZ

//ITEM 7
    tmpx = selfx
    tmpturn = rand
    tmpargument = rand % 13
    SetState
    IfStateIs0			 //Rusty Sword
      tmpargument = 44
    IfStateIs1			 //Torch
      tmpargument = 37
    IfStateIs2			 //Pick
      tmpargument = 48
    IfStateIs3			 //Quarter Staff
      tmpargument = 47
    IfStateIs4			 //Rusty Shield
      tmpargument = 45
    IfStateIs5		         //Poison Potion
      tmpargument = 69
    IfStateIs6		         //Claw
      tmpargument = 65
    IfStateIs7		         //Oil flask
      tmpargument = 64
    IfStateIs8		         //Identify Scroll
      tmpargument = 88
    IfStateIs9		         //Torch
      tmpargument = 37
    IfStateIs10		         //Shovel
      tmpargument = 149
    IfStateIs11		         //Mushroom
      tmpargument = 152
    IfStateIs12		         //Crowbar
      tmpargument = 157
    SpawnExactCharacterXYZ

//ITEM 8
    tmpx = selfx + 178
    tmpturn = rand
    tmpargument = rand % 13
    SetState
    IfStateIs0			 //Rusty Sword
      tmpargument = 44
    IfStateIs1			 //Torch
      tmpargument = 37
    IfStateIs2			 //Pick
      tmpargument = 48
    IfStateIs3			 //Quarter Staff
      tmpargument = 47
    IfStateIs4			 //Rusty Shield
      tmpargument = 45
    IfStateIs5		         //Poison Potion
      tmpargument = 69
    IfStateIs6		         //Claw
      tmpargument = 65
    IfStateIs7		         //Oil flask
      tmpargument = 64
    IfStateIs8		         //Identify Scroll
      tmpargument = 88
    IfStateIs9		         //Torch
      tmpargument = 37
    IfStateIs10		         //Shovel
      tmpargument = 149
    IfStateIs11		         //Mushroom
      tmpargument = 152
    IfStateIs12		         //Crowbar
      tmpargument = 157
    SpawnExactCharacterXYZ
  tmpargument = 0
  SetState

  tmpargument = 1
  IfArmorIs		//MYLVIN THE ALCHEMIST
//ITEM 0
    tmpturn = rand
    tmpdistance = 0
    tmpx = selfx
    tmpy = selfy
    tmpargument = 71		//Life potion
    SpawnExactCharacterXYZ
//ITEM 1
    tmpturn = rand
    tmpx = selfx - 178
    tmpy = selfy - 178
    tmpargument = 50		//Levitation potion
    SpawnExactCharacterXYZ
//ITEM 2
    tmpx = selfx
    tmpturn = rand
    tmpargument = 86		//Power potion
    SpawnExactCharacterXYZ
//ITEM 3
    tmpx = selfx + 178
    tmpturn = rand
    tmpargument = 81		//Magic resist potion
    SpawnExactCharacterXYZ
//ITEM 4
    tmpy = selfy
    tmpturn = rand
    tmpargument = 70		//Speed potion
    SpawnExactCharacterXYZ
//ITEM 5
    tmpx = selfx - 178
    tmpturn = rand
    tmpargument = 97		//Regeneration potion
    SpawnExactCharacterXYZ
//ITEM 6
    tmpy = selfy + 178
    tmpturn = rand
    tmpargument = 64		//Oil flask
    SpawnExactCharacterXYZ
//ITEM 7
    tmpx = selfx
    tmpturn = rand
    tmpargument = 86		//Power potion
    SpawnExactCharacterXYZ
//ITEM 8
    tmpx = selfx + 178
    tmpturn = rand
    tmpargument = 97		//Regeneration potion
    SpawnExactCharacterXYZ
//ITEM 9
    tmpx = selfx + 278
    tmpy = selfy
    tmpturn = rand
    tmpargument = 81		//Magic resist potion
    SpawnExactCharacterXYZ
//ITEM 10
    tmpx = selfx - 278
    tmpturn = rand
    tmpargument = 50		//Levitation potion
    SpawnExactCharacterXYZ
//ITEM 11
    tmpy = selfy + 278
    tmpturn = rand
    tmpargument = 71		//Life potion
    SpawnExactCharacterXYZ
//ITEM 12
    tmpx = selfx
    tmpturn = rand
    tmpargument = 64		//Oil flask
    SpawnExactCharacterXYZ
//ITEM 13
    tmpx = selfx + 278
    tmpturn = rand
    tmpargument = 70		//Speed potion
    SpawnExactCharacterXYZ

  tmpargument = 2
  IfArmorIs		//ALKOR GNOME SHOP
//ITEM 0
    tmpturn = rand
    tmpdistance = 0
    tmpx = selfx
    tmpy = selfy
    tmpargument = 98		//Pistol
    SpawnExactCharacterXYZ
//ITEM 1
    tmpturn = rand
    tmpx = selfx - 178
    tmpy = selfy - 178
    tmpargument = 85		//Bear trap
    SpawnExactCharacterXYZ
//ITEM 2
    tmpx = selfx
    tmpturn = rand
    tmpargument = 85		//Bear trap
    SpawnExactCharacterXYZ
//ITEM 3
    tmpx = selfx + 178
    tmpturn = rand
    tmpargument = 62		//Crossbow
    SpawnExactCharacterXYZ
//ITEM 6
    tmpy = selfy + 178
    tmpturn = rand
    tmpargument = 64		//Oil flask
    SpawnExactCharacterXYZ
//ITEM 7
    tmpx = selfx + 278
    tmpy = selfy
    tmpturn = rand
    tmpargument = 62		//Crossbow
    SpawnExactCharacterXYZ
//ITEM 8
    tmpx = selfx - 278
    tmpturn = rand
    tmpargument = 118		//Bomb
    SpawnExactCharacterXYZ
//ITEM 9
    tmpy = selfy + 228
    tmpturn = rand
    tmpargument = 100		//Gonne
    SpawnExactCharacterXYZ
//ITEM 10
    tmpx = selfx
    tmpturn = rand
    tmpargument = 123		//Ammo box
    SpawnExactCharacterXYZ
//ITEM 11
    tmpx = selfx + 178
    tmpturn = rand
    tmpargument = 123		//Ammo box
    SpawnExactCharacterXYZ
//ITEM 12
    tmpx = selfx
    tmpy = selfy - 128
    tmpturn = rand
    tmpargument = 100		//Gonne
    SpawnExactCharacterXYZ
//ITEM 13
    tmpx = selfx + 278
    tmpy = selfy + 178
    tmpturn = rand
    tmpargument = 118		//Bomb
    SpawnExactCharacterXYZ

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

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

//------------------------------------------------------------------------------
// ZZ> Move around...  Start at state 0
IfTimeOut

  //SHOPKEEPING
  IfStateIs0
    IfHoldingRangedWeapon
      tmpargument = LATCHPACKRIGHT	//Seathe pistol
      PressLatchButton
    tmpx = rand & 255 - 128 + selfspawnx
    tmpy = rand & 255 - 128 + selfspawny
    ClearWaypoints
    AddWaypoint
    SetTargetToWideEnemy
      tmpargument =  1
      SetState

  //COMBAT
  IfStateIs1
    IfHoldingRangedWeapon
      DoNothing
    Else
      SetTargetToSelf
      tmpargument = [PIST]
      IfHoldingItemID
        tmpargument = ACTIONMC
        DoAction
        tmpargument = [PIST]
        RestockTargetAmmoIDFirst        //Reload gun
        tmpargument = 10
        SendMessageNear
        tmpargument = 6
        PlaySound
      SetTargetToOldTarget

    SetTargetToNearestEnemy
      tmpx = selfx		//Face enemy
      tmpy = selfy
      tmpturn = targetturnto
      tmpdistance = 50
      Compass
      ClearWaypoints
      AddWaypoint
    Else
      tmpargument = 0		//No enemies nearby
      SetState
    IfFacingTarget		//Shoot?
      IfTargetIsOnHatedTeam
        IfHoldingRangedWeapon
          tmpargument = LATCHRIGHT
          PressLatchButton
        Else
          tmpargument = LATCHPACKRIGHT	//Draw pistol
          PressLatchButton
  tmpargument = rand & 31 + 15
  SetTime

//------------------------------------------------------------------------------
// ZZ> Handle death by sending a message and other stuff
IfKilled
  JoinEvilTeam
  tmpargument = 3
  PlaySound

  //Find out who died
  tmpargument = 0		//Olaf died
  IfArmorIs
    tmpargument = 0
  Else
    tmpargument = 1		//Mylvin died
    IfArmorIs
      tmpargument = 9
    Else
      tmpargument = 11		//Alkor died

  IfTargetIsOnSameTeam
    tmpargument = MESSAGEFRAG
    IfTargetIsSelf
      tmpargument = MESSAGEACCIDENT
  SendMessage

  // Drop goodies
  tmpargument = selfmoney
  DropMoney
  DropItems

  // Make the character body
  tmpargument = 45
  SetBumpHeight

//------------------------------------------------------------------------------
//Handle being attacked and backstabbed
IfAttacked
  SetTargetToWhoeverAttacked

  //See if he got backstabbed
  IfStateIs0
    IfBackstabbed
      tmpargument = 20
      tmpdistance = EXPMURDER
      GiveExperienceToTarget
      SetTargetToSelf
      KillTarget
      SetTargetToWhoeverAttacked

  tmpargument = 1		//Combat
  SetState
  IfTargetIsOnSameTeam
    tmpargument = 2
    PlaySound
    tmpargument = MESSAGEOUCH
    SendMessageNear
    JoinEvilTeam
  Else
    tmpargument = rand & 1 + 4
    PlaySound

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