diff --git a/source/blood/src/eventq.cpp b/source/blood/src/eventq.cpp index 9a348539b..4ef146ac2 100644 --- a/source/blood/src/eventq.cpp +++ b/source/blood/src/eventq.cpp @@ -37,7 +37,7 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. class EventQueue { public: - PriorityQueue* PQueue; + PriorityQueue* PQueue; EventQueue() { PQueue = NULL; @@ -48,8 +48,7 @@ public: } EVENT ERemove(void) { - unsigned int node = PQueue->Remove(); - return *(EVENT*)&node; + return PQueue->Remove(); } void Kill(int, int); void Kill(int, int, CALLBACK_ID); @@ -58,19 +57,13 @@ public: EventQueue eventQ; void EventQueue::Kill(int a1, int a2) { - EVENT evn = { (unsigned int)a1, (unsigned int)a2, 0, 0 }; - //evn.at0_0 = a1; - //evn.at1_5 = a2; - - short vs = *(short*)&evn; - PQueue->Kill([=](unsigned int nItem)->bool {return !memcmp(&nItem, &vs, 2); }); + PQueue->Kill([=](EVENT nItem)->bool {return nItem.index == a1 && nItem.type == a2; }); } void EventQueue::Kill(int a1, int a2, CALLBACK_ID a3) { EVENT evn = { (unsigned int)a1, (unsigned int)a2, kCommandCallback, (unsigned int)a3 }; - unsigned int vc = *(unsigned int*)&evn; - PQueue->Kill([=](unsigned int nItem)->bool {return nItem == vc; }); + PQueue->Kill([=](EVENT nItem)->bool {return !memcmp(&nItem, &evn, sizeof(EVENT)); }); } //struct RXBUCKET @@ -276,9 +269,9 @@ void evInit(void) if (eventQ.PQueue) delete eventQ.PQueue; if (VanillaMode()) - eventQ.PQueue = new VanillaPriorityQueue(); + eventQ.PQueue = new VanillaPriorityQueue(); else - eventQ.PQueue = new StdPriorityQueue(); + eventQ.PQueue = new StdPriorityQueue(); eventQ.PQueue->Clear(); int nCount = 0; for (int i = 0; i < numsectors; i++) @@ -481,22 +474,22 @@ void evPost(int nIndex, int nType, unsigned int nDelta, COMMAND_ID command) command = evGetSourceState(nType, nIndex) ? COMMAND_ID_1 : COMMAND_ID_0; else if (command == COMMAND_ID_4) command = evGetSourceState(nType, nIndex) ? COMMAND_ID_0 : COMMAND_ID_1; - EVENT evn; + EVENT evn = {}; evn.index = nIndex; evn.type = nType; evn.cmd = command; // Inlined? - eventQ.PQueue->Insert(gFrameClock+nDelta, *(unsigned int*)&evn); + eventQ.PQueue->Insert(gFrameClock+nDelta, evn); } void evPost(int nIndex, int nType, unsigned int nDelta, CALLBACK_ID a4) { - EVENT evn; + EVENT evn = {}; evn.index = nIndex; evn.type = nType; evn.cmd = kCommandCallback; evn.funcID = a4; - eventQ.PQueue->Insert(gFrameClock+nDelta, *(unsigned int*)&evn); + eventQ.PQueue->Insert(gFrameClock+nDelta, evn); } void evProcess(unsigned int nTime) @@ -563,18 +556,18 @@ void EventQLoadSave::Load() delete eventQ.PQueue; Read(&eventQ, sizeof(eventQ)); if (VanillaMode()) - eventQ.PQueue = new VanillaPriorityQueue(); + eventQ.PQueue = new VanillaPriorityQueue(); else - eventQ.PQueue = new StdPriorityQueue(); + eventQ.PQueue = new StdPriorityQueue(); int nEvents; Read(&nEvents, sizeof(nEvents)); for (int i = 0; i < nEvents; i++) { - EVENT event; + EVENT event = {}; unsigned int eventtime; Read(&eventtime, sizeof(eventtime)); Read(&event, sizeof(event)); - eventQ.PQueue->Insert(eventtime, *(unsigned int*)&event); + eventQ.PQueue->Insert(eventtime, event); } Read(rxBucket, sizeof(rxBucket)); Read(bucketHead, sizeof(bucketHead)); @@ -597,7 +590,7 @@ void EventQLoadSave::Save() dassert(eventQ.PQueue->Size() == 0); for (int i = 0; i < nEvents; i++) { - eventQ.PQueue->Insert(eventstime[i], *(unsigned int*)&events[i]); + eventQ.PQueue->Insert(eventstime[i], events[i]); } Write(rxBucket, sizeof(rxBucket)); Write(bucketHead, sizeof(bucketHead)); diff --git a/source/blood/src/eventq.h b/source/blood/src/eventq.h index e47f1592a..fc9d28324 100644 --- a/source/blood/src/eventq.h +++ b/source/blood/src/eventq.h @@ -52,11 +52,11 @@ enum COMMAND_ID { }; struct EVENT { - unsigned int index : 13; // index + unsigned int index : 14; // index unsigned int type : 3; // type unsigned int cmd : 8; // cmd unsigned int funcID : 8; // callback -}; // <= 4 bytes +}; void evInit(void); char evGetSourceState(int nType, int nIndex); diff --git a/source/blood/src/pqueue.cpp b/source/blood/src/pqueue.cpp index f411f1215..e59d502f8 100644 --- a/source/blood/src/pqueue.cpp +++ b/source/blood/src/pqueue.cpp @@ -20,128 +20,5 @@ along with this program; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */ //------------------------------------------------------------------------- -#include "common_game.h" -#include "pqueue.h" -PriorityQueue::~PriorityQueue() -{ -} - -VanillaPriorityQueue::~VanillaPriorityQueue() -{ -} - -void VanillaPriorityQueue::Clear(void) -{ - fNodeCount = 0; - memset(queueItems, 0, sizeof(queueItems)); -} - -void VanillaPriorityQueue::Upheap(void) -{ - queueItem item = queueItems[fNodeCount]; - queueItems[0].at0 = 0; - unsigned int x = fNodeCount; - while (queueItems[x>>1] > item) - { - queueItems[x] = queueItems[x>>1]; - x >>= 1; - } - queueItems[x] = item; -} - -void VanillaPriorityQueue::Downheap(unsigned int n) -{ - queueItem item = queueItems[n]; - while (fNodeCount/2 >= n) - { - unsigned int t = n*2; - if (t < fNodeCount && queueItems[t] > queueItems[t+1]) - t++; - if (item <= queueItems[t]) - break; - queueItems[n] = queueItems[t]; - n = t; - } - queueItems[n] = item; -} - -void VanillaPriorityQueue::Delete(unsigned int k) -{ - dassert(k <= fNodeCount); - queueItems[k] = queueItems[fNodeCount--]; - Downheap(k); -} - -void VanillaPriorityQueue::Insert(unsigned int a1, unsigned int a2) -{ - dassert(fNodeCount < kPQueueSize); - fNodeCount++; - queueItems[fNodeCount].at0 = a1; - queueItems[fNodeCount].at4 = a2; - Upheap(); -} - -unsigned int VanillaPriorityQueue::Remove(void) -{ - unsigned int data = queueItems[1].at4; - queueItems[1] = queueItems[fNodeCount--]; - Downheap(1); - return data; -} - -unsigned int VanillaPriorityQueue::LowestPriority(void) -{ - dassert(fNodeCount > 0); - return queueItems[1].at0; -} - -void VanillaPriorityQueue::Kill(std::function pMatch) -{ - for (unsigned int i = 1; i <= fNodeCount;) - { - if (pMatch(queueItems[i].at4)) - Delete(i); - else - i++; - } -} - -StdPriorityQueue::~StdPriorityQueue() -{ - stdQueue.clear(); -} - -void StdPriorityQueue::Clear(void) -{ - stdQueue.clear(); -} - -void StdPriorityQueue::Insert(unsigned int nPriority, unsigned int nData) -{ - stdQueue.insert({ nPriority, nData }); -} - -unsigned int StdPriorityQueue::Remove(void) -{ - dassert(stdQueue.size() > 0); - int nData = stdQueue.begin()->at4; - stdQueue.erase(stdQueue.begin()); - return nData; -} - -unsigned int StdPriorityQueue::LowestPriority(void) -{ - return stdQueue.begin()->at0; -} - -void StdPriorityQueue::Kill(std::function pMatch) -{ - for (auto i = stdQueue.begin(); i != stdQueue.end();) - { - if (pMatch(i->at4)) - i = stdQueue.erase(i); - else - i++; - } -} +// TODO: delete this diff --git a/source/blood/src/pqueue.h b/source/blood/src/pqueue.h index 6979e575f..01fb9df4f 100644 --- a/source/blood/src/pqueue.h +++ b/source/blood/src/pqueue.h @@ -23,12 +23,13 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. #pragma once #include #include +#include "common_game.h" #define kPQueueSize 1024 -struct queueItem +template struct queueItem { - unsigned int at0; // priority - unsigned int at4; // data + uint32_t at0; // priority + T at4; // data bool operator>(const queueItem& other) const { return at0 > other.at0; @@ -55,44 +56,131 @@ struct queueItem } }; -class PriorityQueue +template class PriorityQueue { public: - virtual ~PriorityQueue() = 0; - virtual unsigned int Size(void) = 0; + virtual ~PriorityQueue() {} + virtual uint32_t Size(void) = 0; virtual void Clear(void) = 0; - virtual void Insert(unsigned int, unsigned int) = 0; - virtual unsigned int Remove(void) = 0; - virtual unsigned int LowestPriority(void) = 0; - virtual void Kill(std::function pMatch) = 0; + virtual void Insert(uint32_t, T) = 0; + virtual T Remove(void) = 0; + virtual uint32_t LowestPriority(void) = 0; + virtual void Kill(std::function pMatch) = 0; }; -class VanillaPriorityQueue : public PriorityQueue +template class VanillaPriorityQueue : public PriorityQueue { public: - queueItem queueItems[kPQueueSize + 1]; - unsigned int fNodeCount; // at2008 - ~VanillaPriorityQueue(); - unsigned int Size(void) { return fNodeCount; }; - void Clear(void); - void Upheap(void); - void Downheap(unsigned int); - void Delete(unsigned int); - void Insert(unsigned int, unsigned int); - unsigned int Remove(void); - unsigned int LowestPriority(void); - void Kill(std::function pMatch); + queueItem queueItems[kPQueueSize + 1]; + uint32_t fNodeCount; // at2008 + ~VanillaPriorityQueue() {} + uint32_t Size(void) { return fNodeCount; }; + void Clear(void) + { + fNodeCount = 0; + memset(queueItems, 0, sizeof(queueItems)); + } + void Upheap(void) + { + queueItem item = queueItems[fNodeCount]; + queueItems[0].at0 = 0; + uint32_t x = fNodeCount; + while (queueItems[x>>1] > item) + { + queueItems[x] = queueItems[x>>1]; + x >>= 1; + } + queueItems[x] = item; + } + void Downheap(uint32_t n) + { + queueItem item = queueItems[n]; + while (fNodeCount/2 >= n) + { + uint32_t t = n*2; + if (t < fNodeCount && queueItems[t] > queueItems[t+1]) + t++; + if (item <= queueItems[t]) + break; + queueItems[n] = queueItems[t]; + n = t; + } + queueItems[n] = item; + } + void Delete(uint32_t k) + { + dassert(k <= fNodeCount); + queueItems[k] = queueItems[fNodeCount--]; + Downheap(k); + } + void Insert(uint32_t a1, T a2) + { + dassert(fNodeCount < kPQueueSize); + fNodeCount++; + queueItems[fNodeCount].at0 = a1; + queueItems[fNodeCount].at4 = a2; + Upheap(); + } + T Remove(void) + { + T data = queueItems[1].at4; + queueItems[1] = queueItems[fNodeCount--]; + Downheap(1); + return data; + } + uint32_t LowestPriority(void) + { + dassert(fNodeCount > 0); + return queueItems[1].at0; + } + void Kill(std::function pMatch) + { + for (unsigned int i = 1; i <= fNodeCount;) + { + if (pMatch(queueItems[i].at4)) + Delete(i); + else + i++; + } + } }; -class StdPriorityQueue : public PriorityQueue +template class StdPriorityQueue : public PriorityQueue { public: - std::multiset stdQueue; - ~StdPriorityQueue(); - unsigned int Size(void) { return stdQueue.size(); }; - void Clear(void); - void Insert(unsigned int, unsigned int); - unsigned int Remove(void); - unsigned int LowestPriority(void); - void Kill(std::function pMatch); + std::multiset> stdQueue; + ~StdPriorityQueue() + { + stdQueue.clear(); + } + uint32_t Size(void) { return stdQueue.size(); }; + void Clear(void) + { + stdQueue.clear(); + } + void Insert(uint32_t nPriority, T data) + { + stdQueue.insert({ nPriority, data }); + } + T Remove(void) + { + dassert(stdQueue.size() > 0); + T data = stdQueue.begin()->at4; + stdQueue.erase(stdQueue.begin()); + return data; + } + uint32_t LowestPriority(void) + { + return stdQueue.begin()->at0; + } + void Kill(std::function pMatch) + { + for (auto i = stdQueue.begin(); i != stdQueue.end();) + { + if (pMatch(i->at4)) + i = stdQueue.erase(i); + else + i++; + } + } };