• Welcome to Valhalla Legends Archive.
 

Bot Queue

Started by Sargera, June 23, 2004, 02:53 AM

Previous topic - Next topic

Sargera

Has anyone used the STL queue for their bot queue?  Also, if anyone could be as so kind to, please post the source code to your queue implementation.  I'd like to see some examples so I can compare it to mine which I have in the works.  Thanks!  :)

MyndFyre

Quote from: Sargera on June 23, 2004, 02:53 AM
Has anyone used the STL queue for their bot queue?  Also, if anyone could be as so kind to, please post the source code to your queue implementation.  I'd like to see some examples so I can compare it to mine which I have in the works.  Thanks!  :)

Queues are not hard to implement, particularly if you figure that it's just a linked list.  The fact that you don't need to implement the insert method makes it even easier.

Let's say your Bnet packet structure is defined:


typedef struct tagBNET_MESSAGE {
 // ... stuff
} BNET_MESSAGE, *PBNET_MESSAGE;


Header:

typedef struct tagMSG_NODE
{
  PBNET_MESSAGE msg;
 // this struct provides for a singly-linked list.  a doubly-linked list would be more efficient, especially for remove operations
 // that occur at the end of the list.  HOWEVER, since we don't need to worry about that (all remove operations will occur
 // at the beginning of the list), a singly-linked list will suffice for this example.
  tagMSG_NODE *next;
} MSG_NODE, *PMSG_NODE;
public class BnetMsgList
{
 protected:
   int m_nCount;
   PMSG_NODE m_pHead, m_pTail;

 public:
   bool add(PBNET_MESSAGE msg);
   bool get(int index, PBNET_MESSAGE msg); // msg is an [Out] parameter.
   bool removeAt(int index);
   inline int getCount();
}
public class BnetMsgQueue
{
 protected:
   BnetMsgList *m_pMsgList;
 public:
   bool enqueue(PBNET_MESSAGE msg);
   bool dequeue(PBNET_MESSAGE msg); // msg in an [Out] parameter.
   inline bool hasItems();
}


Implementation (off the top of my head.  I don't claim to be a C++ programmer and this is untested.  Tell me how it comes out if anyone tries it ;) ):

bool BnetMsgList::add(PBNET_MESSAGE msg)
{
 PMSG_NODE pNewNode;
 pNewNode->msg = msg;

 if (pHead == NULL)
 {
   pHead = pTail = pNewNode;
 }
 else
 {
   pTail->next = pNewNode;
   pTail = pNewNode;
 }
 m_nCount++;

 return true;
}
bool BnetMsgList::get(int index, PBNET_MESSAGE msg)
{
 if ((index >= m_nCount) || (pHead == NULL))
   return false;
 else
 {
   PMSG_NODE pCursor;
   int nIdx = 0;
   do
   {
     if (pCursor == NULL)
       pCursor = pHead;
     else
       pCursor = pCursor->next;

     if (nIdx == index)
       msg = pCursor->msg;

   } while (pCursor != NULL);
 }
 if (msg != NULL)
   return true;
}
bool BnetMsgList::removeAt(int index)
{
 bool bRemoved = false;
 if (pHead != NULL)
 {
   if (index == 0)
   {
     PMSG_NODE pCursor = pHead;
     pHead = pHead->next;
     delete pCursor;
     bRemoved = true;
     m_nCount--;
   }
   else
   {
     PMSG_NODE pPrev, pCur;
     pCur = pHead->next;
     pPrev = pHead;
     if (pCur != NULL)
     {
       int nIdx = 1;
       do
       {
         if (nIdx == index)
         {
           pPrev->next = pCur->next;
           delete pCur;
           bRemoved = true;
           m_nCount--;
           break;
         }
         else
         {
           pPrev = pCur;
           pCur = pCur->next;
         }
       } while (pCur != NULL);
     }
   }
 }
 return bRemoved;
}
inline int BnetMsgList::getCount()
{
 return m_nCount;
}

bool BnetMsgQueue::enqueue(PBNET_MESSAGE msg)
{
 return m_pMsgList->add(msg);
}
bool BnetMsgQueue::dequeue(PBNET_MESSAGE msg) // out
{
 bool bSuccess = m_pMsgList->get(0, msg);
 if (bSuccess)
   m_pMsgList->removeAt(0);
 return bSuccess;
}
inline bool BnetMsgQueue::hasItems()
{
 return (m_pMsgList->getCount() > 0);
}



Anyway, that's what I came up with.  List I said, I'm not a C++ programmer, and this is untested.  I am definitely interested to know what others came up with or how this turns out (cuz I don't plan on testing it, either).

Cheers.
QuoteEvery generation of humans believed it had all the answers it needed, except for a few mysteries they assumed would be solved at any moment. And they all believed their ancestors were simplistic and deluded. What are the odds that you are the first generation of humans who will understand reality?

After 3 years, it's on the horizon.  The new JinxBot, and BN#, the managed Battle.net Client library.

Quote from: chyea on January 16, 2009, 05:05 PM
You've just located global warming.

Sargera

Thanks Myndfyre, but I don't think I'll be using that.  I'd prefer to use the queue provided in the STL if possible for just cleaner code, and better design.  I don't care too much about customizing the queue for a specific thing, as it's one of the least important things about the bot IMO.  However, I'd like to know if it's possible to set delays on when elements are processed in the queue (messages).  Since the queue is obviously designed for speed (one of the major notions of the STL) it's going to send those messages I insert into the queue as fast as possible to Battle.net, which comes at a cost of an IPBan which is what we're trying to avoid in the queue system (among other things).

Bottom line question:  How do you set delays in the queue provided in the STL?

MyndFyre

If you're using Windows, you want to set up your bot to respond to the WM_TIMER message and set up timers for how often to dequeue a packet.  Within the timer callback, send.
QuoteEvery generation of humans believed it had all the answers it needed, except for a few mysteries they assumed would be solved at any moment. And they all believed their ancestors were simplistic and deluded. What are the odds that you are the first generation of humans who will understand reality?

After 3 years, it's on the horizon.  The new JinxBot, and BN#, the managed Battle.net Client library.

Quote from: chyea on January 16, 2009, 05:05 PM
You've just located global warming.