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

  //Make it a shop
  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
  
  //Prepeare the random inventory list
  tmpy = 0
  
  //Slot 0
  tmpargument = 0   
  tmpx = 50         //Sword
  SetXY
  
  //Slot 1
  tmpargument = 1   
  tmpx = 51         //Spear
  SetXY
  
  //Slot 2
  tmpargument = 2   
  tmpx = 74         //Stiletto
  SetXY
  
  //Slot 3
  tmpargument = 3   
  tmpx = 90         //Quiver
  SetXY
  
  //Slot 4
  tmpargument = 4   
  tmpx = 96         //Bomb
  SetXY
  
  //Slot 5
  tmpargument = 5   
  tmpx = 78         //Kite shield
  SetXY
  
  //Slot 6
  tmpargument = 6   
  tmpx = 51         //Spear - TWICE for X2 chance
  SetXY
  
  //Slot 7
  tmpargument = 7   
  tmpx = 92         //Scimitar
  SetXY
  
  //Slot 8
  tmpargument = 8   
  tmpx = 92         //Scimitar - TWICE for X2 chance
  SetXY
  
  //Slot 9
  tmpargument = 9   
  tmpx = 71         //Crossbow
  SetXY
  
  //Slot 10
  tmpargument = 10  
  tmpx = 57         //Trident
  SetXY

  //Slot 11
  tmpargument = 11  
  tmpx = 57         //Trident - TWICE for X2 chance
  SetXY

  //Slot 12
  tmpargument = 12  
  tmpx = 131         //Scythe - TWICE for X2 chance
  SetXY

  //Slot 13
  tmpargument = 13  
  tmpx = 131        //Scythe
  SetXY

  //Slot 14
  tmpargument = 14  
  tmpx = 123         //Ammobox
  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
  
  //And finally spawn a special magic item
  tmpx = selfspawnx
  tmpy = selfspawny
  tmpturn = rand
  tmpargument = rand % 3
  SetState
  IfStateIs0
    tmpargument = 62    //Thunder spear
  IfStateIs1
    tmpargument = 82    //Sojurn
  IfStateIs2
    tmpargument = 83    //Strike
  IfStateIs3
    tmpargument = 89    //The Pike
  SpawnExactCharacterXYZ
  
  //Dont run around
  Sneak				
  tmpargument = STATEGUARD
  SetState
  
  
//------------------------------------------------------------------------------
IfHealed
  tmpargument = 1
  SendMessageNear
  
//------------------------------------------------------------------------------
//Use scimitar to parry!
IfStateIsCombat
  IfTargetIsOnHatedTeam
    IfTargetIsAttacking
      IfFacingTarget
        tmpx = targetdistance
        tmpy = 200
        IfXIsLessThanY
          tmpargument = STATEPARRY
          SetState
IfStateIsParry
  SetTargetToWideEnemy
  SetTurnModeToWatchTarget
  tmpargument = ACTIONPC
  DoAction
  KeepAction
  tmpargument = 25
  SetTime
  
//------------------------------------------------------------------------------
// 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
  
  //Stop parrying
  IfStateIsParry
    tmpargument = STATECOMBAT
    SetState
    SetTurnModeToVelocity

  //Shopkeeping
  IfStateIsGuard
    SetTargetToWideEnemy		//Enemy near, go combat mode
      tmpargument = STATECOMBAT
      SetState
      CallForHelp
    Else
      tmpx = rand & 511 + selfspawnx - 256
      tmpy = rand & 511 + selfspawny - 256
      ClearWaypoints
      AddWaypoint

      //Reduce that counter
      tmpx = selfcontent
      tmpy = 0
      IfXIsMoreThanY
        tmpargument = selfcontent - 1
        SetContent
          
  //Defend shop
  IfStateIsCombat
    Run
    SetTargetToWideEnemy
      CallForHelp

      //Combat position
      tmpx = targetx
      tmpy = targety
      tmpdistance = 500
      tmpturn = targetturnto
      Compass
      ClearWaypoints
      AddWaypoint
      tmpargument = 25
      SetTime
      
      //Melee attacks
      IfFacingTarget
        tmpx = targetdistance
        tmpy = 200
        IfXIsLessThanY
          IfHoldingMeleeWeapon
            PressLatchButton
        Else
          IfHoldingRangedWeapon
            PressLatchButton
      
      //Draw weapons
      IfUnarmed
        tmpargument = LATCHPACKRIGHT      //Scimitar
        PressLatchButton
      Else
        IfHoldingRangedWeapon
          DoNothing
        Else
          tmpargument = LATCHPACKLEFT     //Xbow
          PressLatchButton
          
        
    //No nearby enemies
    Else    
      tmpargument = STATEGUARD
      SetState        
    

//------------------------------------------------------------------------------
//What to do if bumped
IfBumped
  IfStateIsGuard
    SetTargetToWhoeverBumped
    tmpargument = 0
    IfContentIs
      tmpargument = 2
      SendMessageNear
      PlaySound
      tmpargument = 6
      SetContent

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