Code: Select all
|********************************************************************|
| Module: PriorityQueue v0.7 by Praxiiz Spectreon |
|********************************************************************|
| |
| Purpose: This module implements a priority queue for use in |
| processing commands. It is intended for use in a bot |
| that needs to react to commands based firstly on priority |
| and secondly on the order that the commands were received.|
| |
| Each command shall have five properties: command (string),|
| target (string), priority (integer), mana requirement |
| (integer), and health requirement (integer). |
| |
| To Do: qRemove, find a generic way to declare multiple queues |
| |
| Notes: A plugin would be more efficient and appropriate, however,|
| this was made primarily for an eqemulator server that has |
| a very tight list of approved plugins. |
| |
| Usage: To use this queue in your code please keep in mind the |
| following: |
| (1) An include statement is needed with the filename used |
| to save this code. For example: |
| #include pQueue.inc |
| |
| (2) The queue must be initialized before using it. This |
| is accomplished with the following command: |
| /call qInit |
| |
| (3) The following commands are legal: |
| |
| qSearch(command, target) |
| qAdd(pItem, pTarget, priority, mana, health) |
| qClear |
| qPop |
| qPeak |
| |
| (4) When a qPop or qPeak is called, the resulting command |
| is placed in the following variables: |
| |
| rCommand |
| rTarget |
| rPriority |
| rMana |
| rHealth |
| |
|********************************************************************|
#define QUEUE_MAX_SIZE 10
|********************************************************************|
| Function: qInit |
| Purpose: Initializes the queue and prepares it for use. |
| |
|********************************************************************|
Sub qInit(pItem, pTarget, int pPriority, int pMana, int pHealth)
|** arrays used as memory for the linked list **|
/declare nextQueue[QUEUE_MAX_SIZE] int outer 0
/declare prevQueue[QUEUE_MAX_SIZE] int outer 0
/declare cmdQueue[QUEUE_MAX_SIZE] string outer
/declare priorityQueue[QUEUE_MAX_SIZE] int outer 0
/declare targetQueue[QUEUE_MAX_SIZE] string outer
/declare manaQueue[QUEUE_MAX_SIZE] int outer 0
/declare healthQueue[QUEUE_MAX_SIZE] int outer 0
/declare queueSize int outer 0
|** Linked List Header **|
/declare listHead int outer 0
|** Resulting command will be placed in the following variables **|
/declare rCommand string outer
/declare rTarget string outer
/declare rPriority int outer 0
/declare rMana int outer 0
/declare rHealth int outer 0
/call qClear
/return
|********************************************************************|
| Function: qAdd |
| Purpose: Adds an item to the queue, increases queue size and |
| adjusts queue pointers. |
| |
|********************************************************************|
Sub qAdd(pItem, pTarget, int pPriority, int pMana, int pHealth)
/if (${queueSize} < QUEUE_MAX_SIZE) {
/if (${queueSize} > 0) {
/declare emptySpace int local 0
|** find some empty space **|
/declare i int local 1
/for i 1 to QUEUE_MAX_SIZE
/if (${nextQueue[${i}]} == 0) {
/varset emptySpace ${i}
} else {
/next i
}
|** find the end of the list **|
/declare endOfList int local 1
/varset endOfList ${listHead}
:Link_Loop
/if (${nextQueue[${endOfList}]} > 0) {
/varset endOfList ${nextQueue[${endOfList}]}
/goto :Link_Loop
}
/varset nextQueue[${i}] -1
/varset nextQueue[${endOfList}] ${i}
/varset prevQueue[${i}] ${endOfList}
/varcalc queueSize ${queueSize}+1
/varset cmdQueue[${i}] ${pItem}
/varset targetQueue[${i}] ${pTarget}
/varset priorityQueue[${i}] ${pPriority}
/varset manaQueue[${i}] ${pMana}
/varset healthQueue[${i}] ${pHealth}
} else {
|** Queue is empty, begin at element 1 **|
/varset listHead 1
/varset nextQueue[1] -1
/varset prevQueue[1] -1
/varset queueSize 1
/varset cmdQueue[1] ${pItem}
/varset targetQueue[1] ${pTarget}
/varset priorityQueue[1] ${pPriority}
/varset manaQueue[1] ${pMana}
/varset healthQueue[1] ${pHealth}
}
}
/return
|********************************************************************|
| Function: qClear |
| Purpose: Clears the queue by setting the size to 0 and resetting |
| queue pointers |
|********************************************************************|
sub qClear
/declare i int local 1
/for i 1 to QUEUE_MAX_SIZE
/varset nextQueue[${i}] 0
/next i
/varset queueSize 0
/return
|********************************************************************|
| Function: qPop |
| Purpose: Pops an element from the queue, first by priority and |
| then by order closest to the front of the queue. |
|********************************************************************|
Sub qPop
/if (${queueSize} == 1) {
/varset rCommand ${cmdQueue[${listHead}]}
/varset rTarget ${targetQueue[${listHead}]}
/varset rPriority ${priorityQueue[${listHead}]}
/varset rMana ${manaQueue[${listHead}]}
/varset rHealth ${healthQueue[${listHead}]}
/varset nextQueue[${listHead}] 0
/varset queueSize 0
/varset listHead 0
} else {
/if (${queueSize} > 0) {
/declare qIterator int local 1
/declare highestPriority int local 0
/declare highestPriorityIdx int local 0
/varset qIterator ${listHead}
:qPop_List
/if (${priorityQueue[${qIterator}]} > ${highestPriority}) {
/varset highestPriorityIdx ${qIterator}
/varset highestPriority ${priorityQueue[${qIterator}]}
}
/if (${nextQueue[${qIterator}]} > 0) {
/varset qIterator ${nextQueue[${qIterator}]}
/goto :qPop_List
}
/varset rCommand ${cmdQueue[${highestPriorityIdx}]}
/varset rTarget ${targetQueue[${highestPriorityIdx}]}
/varset rPriority ${priorityQueue[${highestPriorityIdx}]}
/varset rMana ${manaQueue[${highestPriorityIdx}]}
/varset rHealth ${healthQueue[${highestPriorityIdx}]}
/if (${prevQueue[${highestPriorityIdx}]} > 0) {
/varset nextQueue[${prevQueue[${highestPriorityIdx}]}] ${nextQueue[${highestPriorityIdx}]}
} else {
/varset listHead ${nextQueue[${highestPriorityIdx}]}
}
/if (${nextQueue[${highestPriorityIdx}]} > 0) {
/varset prevQueue[${nextQueue[${highestPriorityIdx}]}] ${prevQueue[${highestPriorityIdx}]}
}
/varset nextQueue[${highestPriorityIdx}] 0
/varcalc queueSize ${queueSize}-1
}
}
/return
|********************************************************************|
| Function: qPeak |
| Purpose: Sets the return variables for an element from the queue, |
| first by priority and then by order closest to the front |
| of the queue. Does not remove the item from the queue. |
| |
|********************************************************************|
Sub qPeak
/if (${queueSize} == 1) {
/varset rCommand ${cmdQueue[${listHead}]}
/varset rTarget ${targetQueue[${listHead}]}
/varset rPriority ${priorityQueue[${listHead}]}
/varset rMana ${manaQueue[${listHead}]}
/varset rHealth ${healthQueue[${listHead}]}
} else {
/if (${queueSize} > 0) {
/declare qIterator int local 1
/declare highestPriority int local 0
/declare highestPriorityIdx int local 0
/varset qIterator ${listHead}
:qPop_List
/if (${priorityQueue[${qIterator}]} > ${highestPriority}) {
/varset highestPriorityIdx ${qIterator}
/varset highestPriority ${priorityQueue[${qIterator}]}
}
/if (${nextQueue[${qIterator}]} > 0) {
/varset qIterator ${nextQueue[${qIterator}]}
/goto :qPop_List
}
/varset rCommand ${cmdQueue[${highestPriorityIdx}]}
/varset rTarget ${targetQueue[${highestPriorityIdx}]}
/varset rPriority ${priorityQueue[${highestPriorityIdx}]}
/varset rMana ${manaQueue[${highestPriorityIdx}]}
/varset rHealth ${healthQueue[${highestPriorityIdx}]}
}
}
/return
|********************************************************************|
| Function: qSearch |
| Purpose: Searches for a command based on command string, and |
| the target string. Returns 0 if the command was not |
| found, 1 if successfully found. |
|********************************************************************|
Sub qSearch(pCommand, pTarget)
/declare resultVal int local 0
/declare qIterator int local ${listHead}
:qSearch_list
/if (${cmdQueue[${qIterator}].Equal[${pCommand}]}) {
/if (${targetQueue[${qIterator}].Equal[${pTarget}]}) {
/varset resultVal 1
}
}
/if (${nextQueue} > 0) {
/if (${resultVal]} < 1) {
/varset qIterator ${nextQueue[${qIterator}]}
/goto :qSearch_List
}
}
/return ${resultVal}

