Skip to content
Snippets Groups Projects
Commit 1abdceb3 authored by David Bergström's avatar David Bergström
Browse files

Remove BuildOrder and BuildOrderQueue

parent 8e7849e6
No related branches found
No related tags found
No related merge requests found
#include "BuildOrder.h"
BuildOrder::BuildOrder()
{
}
BuildOrder::BuildOrder(const std::vector<MetaType> & vec)
: m_buildOrder(vec)
{
}
void BuildOrder::add(const MetaType & type)
{
m_buildOrder.push_back(type);
}
size_t BuildOrder::size() const
{
return m_buildOrder.size();
}
const MetaType & BuildOrder::operator [] (const size_t & index) const
{
return m_buildOrder[index];
}
MetaType & BuildOrder::operator [] (const size_t & index)
{
return m_buildOrder[index];
}
\ No newline at end of file
#pragma once
#include "Common.h"
#include "MetaType.h"
class BuildOrder
{
std::vector<MetaType> m_buildOrder;
public:
BuildOrder();
BuildOrder(const std::vector<MetaType> & buildVector);
void add(const MetaType & type);
size_t size() const;
const MetaType & operator [] (const size_t & index) const;
MetaType & operator [] (const size_t & index);
};
#include "BuildOrderQueue.h"
#include "IDABot.h"
BuildOrderQueue::BuildOrderQueue(IDABot & bot)
: m_bot(bot)
, m_highestPriority(0)
, m_lowestPriority(0)
, m_defaultPrioritySpacing(10)
, m_numSkippedItems(0)
{
}
void BuildOrderQueue::clearAll()
{
// clear the queue
m_queue.clear();
// reset the priorities
m_highestPriority = 0;
m_lowestPriority = 0;
}
BuildOrderItem & BuildOrderQueue::getHighestPriorityItem()
{
// reset the number of skipped items to zero
m_numSkippedItems = 0;
// the queue will be sorted with the highest priority at the back
return m_queue.back();
}
BuildOrderItem & BuildOrderQueue::getNextHighestPriorityItem()
{
assert(m_queue.size() - 1 - m_numSkippedItems >= 0);
// the queue will be sorted with the highest priority at the back
return m_queue[m_queue.size() - 1 - m_numSkippedItems];
}
void BuildOrderQueue::skipItem()
{
// make sure we can skip
assert(canSkipItem());
// skip it
m_numSkippedItems++;
}
bool BuildOrderQueue::canSkipItem()
{
// does the queue have more elements
bool bigEnough = m_queue.size() > (size_t)(1 + m_numSkippedItems);
if (!bigEnough)
{
return false;
}
// is the current highest priority item not blocking a skip
bool highestNotBlocking = !m_queue[m_queue.size() - 1 - m_numSkippedItems].blocking;
// this tells us if we can skip
return highestNotBlocking;
}
void BuildOrderQueue::queueItem(const BuildOrderItem & b)
{
// if the queue is empty, set the highest and lowest priorities
if (m_queue.empty())
{
m_highestPriority = b.priority;
m_lowestPriority = b.priority;
}
// push the item into the queue
if (b.priority <= m_lowestPriority)
{
m_queue.push_front(b);
}
else
{
m_queue.push_back(b);
}
// if the item is somewhere in the middle, we have to sort again
if ((m_queue.size() > 1) && (b.priority < m_highestPriority) && (b.priority > m_lowestPriority))
{
// sort the list in ascending order, putting highest priority at the top
std::sort(m_queue.begin(), m_queue.end());
}
// update the highest or lowest if it is beaten
m_highestPriority = (b.priority > m_highestPriority) ? b.priority : m_highestPriority;
m_lowestPriority = (b.priority < m_lowestPriority) ? b.priority : m_lowestPriority;
}
void BuildOrderQueue::queueAsHighestPriority(const MetaType & type, bool blocking)
{
// the new priority will be higher
int newPriority = m_highestPriority + m_defaultPrioritySpacing;
// queue the item
queueItem(BuildOrderItem(type, newPriority, blocking));
}
void BuildOrderQueue::queueAsLowestPriority(const MetaType & type, bool blocking)
{
// the new priority will be higher
int newPriority = m_lowestPriority - m_defaultPrioritySpacing;
// queue the item
queueItem(BuildOrderItem(type, newPriority, blocking));
}
void BuildOrderQueue::removeHighestPriorityItem()
{
// remove the back element of the vector
m_queue.pop_back();
// if the list is not empty, set the highest accordingly
m_highestPriority = m_queue.empty() ? 0 : m_queue.back().priority;
m_lowestPriority = m_queue.empty() ? 0 : m_lowestPriority;
}
void BuildOrderQueue::removeCurrentHighestPriorityItem()
{
// remove the back element of the vector
m_queue.erase(m_queue.begin() + m_queue.size() - 1 - m_numSkippedItems);
//assert((int)(queue.size()) < size);
// if the list is not empty, set the highest accordingly
m_highestPriority = m_queue.empty() ? 0 : m_queue.back().priority;
m_lowestPriority = m_queue.empty() ? 0 : m_lowestPriority;
}
size_t BuildOrderQueue::size()
{
return m_queue.size();
}
bool BuildOrderQueue::isEmpty()
{
return (m_queue.size() == 0);
}
BuildOrderItem BuildOrderQueue::operator [] (int i)
{
return m_queue[i];
}
std::string BuildOrderQueue::getQueueInformation() const
{
size_t reps = m_queue.size() < 30 ? m_queue.size() : 30;
std::stringstream ss;
// for each unit in the queue
for (size_t i(0); i<reps; i++)
{
const MetaType & type = m_queue[m_queue.size() - 1 - i].type;
ss << type.getName() << "\n";
}
return ss.str();
}
BuildOrderItem::BuildOrderItem(const MetaType & t, int p, bool b)
: type(t)
, priority(p)
, blocking(b)
{
}
bool BuildOrderItem::operator < (const BuildOrderItem & x) const
{
return priority < x.priority;
}
\ No newline at end of file
#pragma once
#include "Common.h"
#include "MetaType.h"
class IDABot;
struct BuildOrderItem
{
MetaType type; // the thing we want to 'build'
int priority; // the priority at which to place it in the queue
bool blocking; // whether or not we block further items
BuildOrderItem(const MetaType & t, int p, bool b);
bool operator<(const BuildOrderItem & x) const;
};
class BuildOrderQueue
{
IDABot & m_bot;
std::deque<BuildOrderItem> m_queue;
int m_lowestPriority;
int m_highestPriority;
int m_defaultPrioritySpacing;
int m_numSkippedItems;
public:
BuildOrderQueue(IDABot & bot);
void clearAll(); // clears the entire build order queue
void skipItem(); // increments skippedItems
void queueAsHighestPriority(const MetaType & type, bool blocking); // queues something at the highest priority
void queueAsLowestPriority(const MetaType & type, bool blocking); // queues something at the lowest priority
void queueItem(const BuildOrderItem & b); // queues something with a given priority
void removeHighestPriorityItem(); // removes the highest priority item
void removeCurrentHighestPriorityItem();
size_t size(); // returns the size of the queue
bool isEmpty();
BuildOrderItem & getHighestPriorityItem(); // returns the highest priority item
BuildOrderItem & getNextHighestPriorityItem(); // returns the highest priority item
bool canSkipItem();
std::string getQueueInformation() const;
// overload the bracket operator for ease of use
BuildOrderItem operator [] (int i);
};
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment