void main()
{
  int nEventNumber = GetUserDefinedEventNumber();
  object oBar = GetNearestObjectByTag("WP_Bar");
  object oBidder = GetLocalObject(OBJECT_SELF,"LastBidder");
  int nHighBid = GetLocalInt(OBJECT_SELF,"HighBid");
  int nBusy = GetLocalInt(OBJECT_SELF,"Busy");

  switch(nEventNumber)
  {

    case 1001: //OnHeartbeat
      if (nBusy == 0)
      {
        SetLocalInt(OBJECT_SELF,"Busy",1);
        int nRandom = d8();
        oBidder = GetNearestObjectByTag("foreign_merchant",OBJECT_SELF,nRandom);
        SetLocalObject(OBJECT_SELF,"LastBidder",oBidder);
        ActionDoCommand(SetFacingPoint(GetPosition(oBidder)));
        if (nHighBid == 0)
        {
          nHighBid = 50;
          ActionDoCommand(PlayVoiceChat(VOICE_CHAT_LOOKHERE));
          ActionWait(2.0f);
          ActionSpeakString("The next slave is up for auction. Who'll start the bidding at 50?");
          ActionDoCommand(SetLocalInt(OBJECT_SELF,"HighBid",nHighBid));
        }
        else
        {
          AssignCommand(oBidder,ActionDoCommand(SetFacingPoint(GetPosition(OBJECT_SELF))));
          AssignCommand(oBidder,ActionDoCommand(PlayVoiceChat(VOICE_CHAT_YES,oBidder)));
          ActionWait(2.0f);
          ActionSpeakString("I've got " + IntToString(nHighBid) + " do I hear " + IntToString(nHighBid+50) + "?");
          nHighBid = nHighBid + 50;
          ActionDoCommand(SetLocalInt(OBJECT_SELF,"HighBid",nHighBid));
        }
        int nFinalPrice = 100 * d10();
        if (nHighBid >= nFinalPrice)
        {
          ActionSpeakString(IntToString(nHighBid - 50) + " going once.");
          ActionWait(2.0f);
          ActionSpeakString(IntToString(nHighBid - 50) + " going twice.");
          ActionWait(2.0f);
          ActionSpeakString(IntToString(nHighBid -50) + " going three times.");
          ActionWait(2.0f);
          ActionDoCommand(PlayVoiceChat(VOICE_CHAT_TASKCOMPLETE));
          ActionWait(2.0f);
          ActionSpeakString("Sold! To the merchant over there for " + IntToString(nHighBid - 50) + "!");
          ActionDoCommand(SetLocalInt(OBJECT_SELF,"HighBid",0));
        }
        ActionDoCommand(SetLocalInt(OBJECT_SELF,"Busy",0));
      }
      break;

  }

}
