//------------------------------------------------------------------------------
IfSpawned
  Walk				//Dont run around
  tmpargument = STATEWANDER
  SetState

  //Setup waypoints
  tmpargument = 0         //Town square
  tmpx = 3000
  tmpy = 4580
  SetXY
  
  tmpargument = 1         //Outside gates
  tmpx = 4300
  tmpy = 4600
  SetXY
  
  tmpargument = 2         //At the well
  tmpx = 2000
  tmpy = 4600
  SetXY
  
  tmpargument = 3         //Kabob shop
  tmpx = 2000
  tmpy = 4200
  SetXY

  tmpargument = 4         //Divine place
  tmpx = 3000
  tmpy = 3700
  SetXY
  
  tmpargument = 5         //Carpet shop
  tmpx = 2700
  tmpy = 4800
  SetXY
  
  tmpargument = 6         //Outside divine place
  tmpx = 2944
  tmpy = 2688
  SetXY

//------------------------------------------------------------------------------
IfHealed
  tmpargument = 7
  SendMessageNear

//------------------------------------------------------------------------------
// Get mean if characters wanna be bad
IfAttacked
  Run
  SetTargetToWhoeverAttacked
  IfTargetIsAPlayer
    CallForHelp
    tmpargument = 5     //Team Evil
    TargetJoinTeam
    tmpargument = STATERETREAT
    SetState
    SetTime
  tmpargument = 2
  PlaySound
IfKilled
  tmpargument = TEAME
  TargetJoinTeam
  CallForHelp			//Warn friends

//------------------------------------------------------------------------------
// ZZ> Handle normal state
IfTimeOut

  //Wander around town
  IfStateIsWander
    SetTargetToWideEnemy		//Enemy near, go combat mode
      tmpargument = STATERETREAT
      SetState
      CallForHelp
    Else
      tmpargument = rand % 7                  //Randomize waypoint
      GetXY
      tmpx = rand & 1023 + tmpx - 512
      tmpy = rand & 1023 + tmpy - 512
      ClearWaypoints
      AddWaypoint
      tmpargument = rand % 150 + 350
      SetTime

          
  //Run away in panic
  IfStateIsRetreat
    Run
    SetTargetToNearbyEnemy
      CallForHelp

    tmpx = rand & 1023 + targetx - 512
    tmpy = rand & 1023 + targety - 512
    tmpdistance = -1500
    tmpturn = targetturnto
    Compass
    ClearWaypoints
    AddWaypoint
    tmpargument = 75
    SetTime
    
    //Return to shopkeeping mode if everything seems normal again
    SetTargetToNearestEnemy
      tmpx = targetdistance
      tmpy = 128*10
      IfXIsMoreThanY
        tmpargument = STATEWANDER
        SetState        
    

//------------------------------------------------------------------------------
//What to do if bumped
IfBumped
  SetTargetToWhoeverBumped
  IfTargetIsAPlayer
    tmpx = rand & 1023 + targetx - 512
    tmpy = rand & 1023 + targety - 512
    ClearWaypoints
    AddWaypoint
    
    //Eek!
    IfTargetIsOnHatedTeam
      tmpargument = STATERETREAT
      SetState
      SetTime

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